i am haveing 3 structures
1 struct1
2 struct2
3 struct 3
i want to send any on of them as an arguments for a function
how can i declare the function prototype
i want to send any 1 structure as arguments
Printable View
i am haveing 3 structures
1 struct1
2 struct2
3 struct 3
i want to send any on of them as an arguments for a function
how can i declare the function prototype
i want to send any 1 structure as arguments
Do you want to use a single function which to work with all of this 3 structures?
As I presume that there is something common in the nature of the structures, I would recommend using classes and inheritance.Quote:
Originally Posted by ravirams
Store common attributes in the base class, and send the base class as a parameter.
and...Code:class CBase
{
public:
void Dance () {};
...
};
Have a Function:Code:class CClass1:public CBase
{
...
};
class CClass2:public CBase
{
...
};
class CClass3:public CBase
{
...
};
Use it this way:Code:void MyFunc (CBase * pBase)
{
pBase->Dance ();
...
}
Note: I have not compiled the above.Code:void CreateObjects ()
{
CClass1 * pC1 = new CClass1 ();
CClass1 * pC2 = new CClass1 ();
// Same function, two dissimilar objects
MyFunc (pC1);
MyFunc (pC2);
delete pC1;
delete pC2;
}
A better sample that explains Inheritance and Polymorphism is here.
You can make them inherit from a base structure so you can treat them polymorphically, in which case the function prototype will use the base structure, or provide an overloaded method each taking a different structure.Quote:
Originally Posted by ravirams
If Virtual Functionalities and Polymorphism have to be used, using a class is best recommended.
Reason:Perhaps, it's a good time to move to classes. ;)Quote:
Originally Posted by MSDN
As that note says, the only difference between structs and classes in C++ are accessors, but I'm not advocating the usage of structs over classes. ;)Quote:
Originally Posted by Siddhartha
PS: I've already taken part in a long debate in the C# forum about structs and classes in C# I'm not going to do it again, so soon... :D
I did not quote you - I was addressing the OP - 'coz he uses struct. ;)Quote:
Originally Posted by cilu
Why would that be?Quote:
Originally Posted by Siddhartha
In C++ there are only two differences between a class and a structure:
- By default, the members of a class are private while the members of a structure are public
- By default, a struct inherits publicly while a class inherits privately
In other words...everything that a class can do, a structure can as well... ;)
The reason is exactly what you mentioned - in C++ (today) a struct is given all the functionalities of a class - baring defaults.Quote:
Originally Posted by Andreas Masur
Given this situation, perhaps, it is best to call a struct being used as a class, a class.
Ahh...okay...I thought I missed some technical reason...however, I still prefer structs over classes if I simply need to hold data...sometimes it is not worth adding the overhead of accessors....granted they will be inlined anyway...Quote:
Originally Posted by Siddhartha
Me too, if holding data is all that it needs to do. :DQuote:
Originally Posted by Andreas Masur
I agree with you there - are accessor methods inlined as a result of optimization (like the rest of code), or does the compiler intelligently detect them as accessors-methods, and inline them intentionally?Quote:
Originally Posted by Andreas Masur
Not sure if I am clear in my query...
They are inlined if you declare them as inline (though you cannot be sure that a function declared as inline will be actually inlined by the compiler), or if you implement them in the class declaration:
Code:class foo
{
int data;
public:
// this is an inline function
int GetData() const
{
return data;
}
};
Guys, sorry for my ignorance, but i think accessor functions re those which re used to access the private data members right (pls correct me if iam wrong)?
Ciao :)
Yes, you are right. :thumb:Quote:
Originally Posted by logan
Presumably: if they are small enough, they become inline.