|
-
April 22nd, 2005, 10:21 AM
#1
structures
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
Last edited by ravirams; April 22nd, 2005 at 10:33 AM.
Reason: some addition
-
April 22nd, 2005, 10:30 AM
#2
Re: structures
Do you want to use a single function which to work with all of this 3 structures?
-
April 22nd, 2005, 10:45 AM
#3
Re: structures
 Originally Posted by ravirams
i want to send any on of them as an arguments for a function
As I presume that there is something common in the nature of the structures, I would recommend using classes and inheritance.
Store common attributes in the base class, and send the base class as a parameter.
Code:
class CBase
{
public:
void Dance () {};
...
};
and...
Code:
class CClass1:public CBase
{
...
};
class CClass2:public CBase
{
...
};
class CClass3:public CBase
{
...
};
Have a Function:
Code:
void MyFunc (CBase * pBase)
{
pBase->Dance ();
...
}
Use it this way:
Code:
void CreateObjects ()
{
CClass1 * pC1 = new CClass1 ();
CClass1 * pC2 = new CClass1 ();
// Same function, two dissimilar objects
MyFunc (pC1);
MyFunc (pC2);
delete pC1;
delete pC2;
}
Note: I have not compiled the above.
A better sample that explains Inheritance and Polymorphism is here.
Last edited by Siddhartha; April 22nd, 2005 at 11:00 AM.
-
April 22nd, 2005, 11:02 AM
#4
Re: structures
 Originally Posted by ravirams
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
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.
-
April 22nd, 2005, 11:08 AM
#5
Re: structures
If Virtual Functionalities and Polymorphism have to be used, using a class is best recommended.
Reason:
 Originally Posted by MSDN
In C++, a structure is the same as a class except that its members are public by default.
Perhaps, it's a good time to move to classes.
-
April 22nd, 2005, 12:47 PM
#6
Re: structures
 Originally Posted by Siddhartha
Perhaps, it's a good time to move to classes. 
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.
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...
-
April 22nd, 2005, 01:21 PM
#7
Re: structures
 Originally Posted by cilu
I'm not advocating the usage of structs over classes. 
I did not quote you - I was addressing the OP - 'coz he uses struct.
-
April 23rd, 2005, 01:12 PM
#8
Re: structures
 Originally Posted by Siddhartha
If Virtual Functionalities and Polymorphism have to be used, using a class is best recommended.
Why would that be?
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...
-
April 23rd, 2005, 02:31 PM
#9
Re: structures
 Originally Posted by Andreas Masur
Why would that be?
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.
Given this situation, perhaps, it is best to call a struct being used as a class, a class.
Last edited by Siddhartha; April 23rd, 2005 at 03:57 PM.
-
April 24th, 2005, 10:59 AM
#10
Re: structures
 Originally Posted by Siddhartha
The reason is exactly what you mentioned - in C++ (today) a struct is given all the functionalities of a class - baring defaults.
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...
-
April 24th, 2005, 11:06 AM
#11
Re: structures
 Originally Posted by Andreas Masur
Ahh...okay...I thought I missed some technical reason...however, I still prefer structs over classes if I simply need to hold data...
Me too, if holding data is all that it needs to do. 
 Originally Posted by Andreas Masur
sometimes it is not worth adding the overhead of accessors....granted they will be inlined anyway...
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?
Not sure if I am clear in my query...
-
April 24th, 2005, 01:33 PM
#12
Re: structures
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;
}
};
-
April 24th, 2005, 04:18 PM
#13
Re: structures
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
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!
-
April 24th, 2005, 04:21 PM
#14
Re: structures
 Originally Posted by logan
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)?
Yes, you are right. 
Presumably: if they are small enough, they become inline.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|