-4

Can i use function overloading with structures as arguments? If yes, how can i implement it? Will this way work?

void function1(struct_a *a, struct_b *b, int i)
{
    cout << "abc";
}

void function1(struct_c *c, struct_d *d)
{
    cout << "def";
}

int main()
{
    struct_a *a = (struct_a *)<some-data-a>;
    struct_b *b = (struct_b *)<some-data-b>;
    struct_c *c = (struct_c *)<some-data-c>;
    struct_d *d = (struct_d *)<some-data-d>;

    function1(a, b, 1);
    return 0;
}
RockyB
  • 59
  • 1
  • 8
  • 4
    Had you created a proper [mcve] instead of hand-wavy pseudo-code, you could have tested it yourself. – StoryTeller - Unslander Monica Apr 30 '19 at 05:31
  • What exactly are you concerned about? – P.W Apr 30 '19 at 05:31
  • Structures are classes in C++ – Aykhan Hagverdili Apr 30 '19 at 05:31
  • @P.W Can i use structures as arguments or only basic data types like int, char, etc work in overloading of functions? – RockyB Apr 30 '19 at 05:33
  • 2
    Your C++ book should hold all the answers about basic language features. If you don't have a book yet, consider browsing this community's [curated book list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Apr 30 '19 at 05:34
  • @RockyB: The function parameters need not be fundamental types. However, as mentioned already, such information is found in books which give you a more structured learning. There is also an [online reference](https://en.cppreference.com/book/intro/function_overloading) which you can refer to. – P.W Apr 30 '19 at 05:39
  • To the OP's defense: The top three google links all have built-in types as arguments, probably for simplicity. But @StoryTeller is right: A working example (which would take about 1 minute to produce from the pseudo code in the post) would answer that question. – Peter - Reinstate Monica Apr 30 '19 at 05:44
  • the problem in your code is not the function calls but a/b/c and d initialization, there are not struct but pointer to struct and `(struct_x *);` cannot be good in the same way `int * i = (int*) ;` cannot be good too. You confuse value and pointer to value. – bruno Apr 30 '19 at 05:49
  • @RockyB Note that for the purpose of creating different types it's ok to simply use empty structs, like `struct struct_a { }; struct struct_b { };` etc. The types are different even if they have identical data (including none at all). – Peter - Reinstate Monica Apr 30 '19 at 05:51

2 Answers2

2

Yes you can. You can overload any function depending upon: 1) number of parameters 2) data type of the parameters

Structures are user defined datatypes, so you can by all means use them to differentiate the different overloaded functions and intermix them with primitive data types as well.

In your case, you can call the 1st function (with 3 parameters) by making a call something like this:

function1(a, b, 1);

For using the 2nd function (with 2 parameters), you simply provide only 2 parameters of struct_c and struct_d:

function1(c, d);
Saket Sharad
  • 392
  • 2
  • 11
1

Yes, of course you can use (pointers to) structs as function arguments, and overload based on them:

void function1(struct_a *a, struct_b *b, int i)
{
    cout << "abc" << endl;
}

void function1(struct_c *c, struct_d *d) {
    cout << "def";
}

int main()
{
    struct_a *a = (struct_a *)<some-data-a>;
    struct_b *b = (struct_b *)<some-data-b>;
    struct_c *c = (struct_c *)<some-data-c>;
    struct_d *d = (struct_d *)<some-data-d>;
    function1(a, b, 1);
    function1(c, d);
    return 0;
}

Output:

abc
def

Live Demo

Any data type, whether fundamental or user-defined, can be used for an argument. You can have as many overloads as you want, as long as they all differ in the number and type(s) of argument that they take.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770