Difference between "C structure" and "C++ structure".
-
The variables in C structure are by default public but in C++ they are private Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
is it true that constructor will be called for class but there will be no constructor for the structure
Structures don't have constructors and how can they be called if they don't exist. Yes but still what you say is correct. It would be better to initialize the member's of the structure somewhere(more preferrably the constructor of the class) before using them. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
Structures don't have constructors and how can they be called if they don't exist. Yes but still what you say is correct. It would be better to initialize the member's of the structure somewhere(more preferrably the constructor of the class) before using them. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
We may have a Constructer's for C++ Structures(look the Following code) C++ Structure's can be initialized/copied by overloading the Assignment Operator Like following
struct Mypoint
{
int x,y;
Mypoint() : x(0),y(0){};
const Mypoint &operator = (const Mypoint &me)
{
x=me.x;
y=me.y;
return *this;
};Knock out 'T' from CAN'T , You 'CAN' if you think you 'CAN' :cool:
-
The variables in C structure are by default public but in C++ they are private Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
_AnShUmAn_ wrote:
The variables in C structure are by default public but in C++ they are private
have you tried that?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
is it true that constructor will be called for class but there will be no constructor for the structure
NiLeSh KoRpE wrote:
there will be no constructor for the structure
Have a try!
#include
#include struct ab
{
ab()
{
printf("alok");
}
};
main()
{
ab a;
int *hello;
printf("Hi, Enter any value > ");
hello=(int*)malloc(sizeof(int));
scanf("%d",hello);
}"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
Structures don't have constructors and how can they be called if they don't exist. Yes but still what you say is correct. It would be better to initialize the member's of the structure somewhere(more preferrably the constructor of the class) before using them. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
_AnShUmAn_ wrote:
It would be better to initialize the member's of the structure
you can create Construtor in Structure, see the example i have quoted in post above you!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
_AnShUmAn_ wrote:
The variables in C structure are by default public but in C++ they are private
have you tried that?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
Ok, I tried your code. Its working fine. Thanks for the update and clarification. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
Ok, I tried your code. Its working fine. Thanks for the update and clarification. Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
_AnShUmAn_ wrote:
Thanks for the update and clarification.
It's OK,I learn a lot while dicussing with you! :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
C allows struct, union, and enum types to be declared within function prototype scope, e.g.: extern void foo(const
struct info { int typ; int sz; }
*s); int bar(struct point { int x, y; }
pt) { ... } C also allows structure types to be declared as function return types, as in: externstruct pt { int x; }
pos(void); C++ does not allow either of these, since the scope of the structure declared in this fashion does not extend outside the function declaration or definition, making it impossible to define objects of that structure type which could be passed as arguments to the function or to assign function return values into objects of that type. Appu.. "If you judge people, you have no time to love them." -
The variables in C structure are by default public but in C++ they are private Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
Hi !!! YOu haven't right.:zzz: By !!! -:KNOX:-