Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Question Function Overloading In C++

Joined
Mar 20, 2023
Messages
3
I was teaching a few of pals how to use C structs today. One of them inquired whether it was possible to return a struct from a function, to which I replied: "No! Instead, you would return pointers to dynamically mallocated structs."
Consider the following:
Code:
struct MyObj{
    double x, y;
};

struct MyObj foo(){
    struct MyObj a;
   
    a.x = 10;
    a.y = 10;
   
    return a;
}       

int main () {

    struct MyObj a;
   
    a = foo();    // This DOES work
    struct b = a; // This does not work
     
    return 0;
}

Coming from someone who typically works in C++, I was anticipating to be unable to return structs by values. With C++, you may overload the operator = for your objects, and it makes perfect sense to have a function that returns your object by value. Nevertheless, in C, you do not have that option, which made me wonder what the compiler is doing.
As stated in the documentation, my issue is: Since the two definitions of foo in the first instance are regarded the same, why isn't the same for foo ptr in the second case? In other words, why is const disregarded in the first example but not in the second?
Any would be appreciated
 
cuppa.drink(java);
Joined
Mar 13, 2018
Messages
7,299
I was teaching a few of pals how to use C structs today. One of them inquired whether it was possible to return a struct from a function, to which I replied: "No! Instead, you would return pointers to dynamically mallocated structs."
Consider the following:
Code:
struct MyObj{
    double x, y;
};

struct MyObj foo(){
    struct MyObj a;
 
    a.x = 10;
    a.y = 10;
 
    return a;
}     

int main () {

    struct MyObj a;
 
    a = foo();    // This DOES work
    struct b = a; // This does not work
   
    return 0;
}

Coming from someone who typically works in C++, I was anticipating to be unable to return structs by values. With C++, you may overload the operator = for your objects, and it makes perfect sense to have a function that returns your object by value. Nevertheless, in C, you do not have that option, which made me wonder what the compiler is doing.
As stated in the documentation, my issue is: Since the two definitions of foo in the first instance are regarded the same, why isn't the same for foo ptr in the second case? In other words, why is const disregarded in the first example but not in the second?
Any would be appreciated
I haven't used C or C++ in like 7 years, so take this with a grain of salt because I didn't even read your question in its entirety.

But I copy pasted your question into ChatGPT and here's it's answer. I take no responsibility on it's correctness:
In C, you can return a struct by value from a function. However, as you correctly pointed out, it is more efficient to return a pointer to a dynamically allocated struct in some cases, particularly when the struct is large.

Regarding your specific question about const, the issue is that in the first example, the const is not part of the function signature. In other words, the compiler treats the following two function declarations as equivalent:

struct MyObj foo();
const struct MyObj foo();

Therefore, when you call the foo() function and assign its return value to a variable, the const qualifier is simply ignored.

On the other hand, in the second example, the const is part of the variable declaration:

const struct MyObj* b = &a;

Here, b is a pointer to a constant struct MyObj. This means that you cannot modify the contents of b using the pointer. If you try to do so, you will get a compiler error.

To fix this, you can either remove the const qualifier from the variable declaration:

struct MyObj* b = &a;

Or remove the const qualifier from the struct definition:

struct MyObj {
double x, y;
};

I hope this helps clarify the issue!
 
Top