• 13
• 15
• 27
• 9
• 9

# Basic C++ question

This topic is 4928 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi ya !Here are some questions : -if i have a class ,lets say A with no default constructor and i declare : A var1; A var2(..,.,..)if i make a call like var1=var2 is the copy constructor called(since var1 hasn't been initialized) or the overloaded = operator.Is that the same as var1(var2)(copy constructor -in my case default copy constructor) I hope someone has understood my confusion. ThankX !

##### Share on other sites
Quote:
 Original post by Hermesif i have a class ,lets say A with no default constructor and i declare : A var1;

If you have no default constructor then

A var1;

will not compile to begin with -- you will get an error. Remember that if you don't define any constructors for your object then a default constructor is automatically generated for your type (which just default constructs all of the bases and members). So your situation is an impossibility.

Quote:
 Original post by Hermesif i make a call likevar1=var2 is the copy constructor called(since var1 hasn't been initialized) or the overloaded = operator.Is that the same as var1(var2)(copy constructor -in my case default copy constructor)

The only time va1 = var2 would call the copy constructor for the type would be if you were initializing it IE

A var1 = var2;

Any other time you use = the assignment operator (operator=) will be called.

##### Share on other sites
I wanted to say that the default constructor doesn't make any initialization.
And another thing if i have lets say a class Base1 and another class Derived1 (derived publicly from Base1)
========Example========class  Base1        class Derived1:public Base1{                     {int x;                  int y;Base1(int t){.....}     Derived1(int t):y(t),::Base1(t){...}  }                     }

is this declaration safe since i don't have pointers in my class(and no copy constructors or =operators overloaded):
Derived1 d1(3);
Derived1 d2(d1);//copiler default copy constructor
Derived d3(10);
d3=d2;//default = operator called

I know this works(but what about style) and that the compiler's default = operator
behaves the same as the copy constructor but is there any difference between them or does it place the same call.

Why can't i make it like this:
Derived1(int t):y(t)/*,::Base1(t)*/{...::Base1(t)} It says something about t redefinition..

Thankx a lot for your help

##### Share on other sites
The thing about not compiling without the default constructor is not so true since i can compile this ,strange isn't it

class Base1{public:	void SetVal(int k){t=k;}private :	int t;};class Derived1:Base1{private:	Base1 b1;public:	void Change(){b1.SetVal(1);}};int main(int argc,char* argv[]){Derived1 dv1;dv1.Change();return 0;}[/suorce]

##### Share on other sites
Quote:
 Original post by HermesI know this works(but what about style) and that the compiler's default = operatorbehaves the same as the copy constructor but is there any difference between them or does it place the same call.

Actually, the default copy constructor and the default assignment operator do NOT behave the same way. The default copy constructor will call the copy constructor of all of the datamembers while the default assignment operator will call all of the assignment operations of all of the datamembers (this is important when working with nonfundamental types). Since you are just showing a plain object with fundamental types as datamembers, this difference shouldn't impact your output as no different user-defined code has to be run to ensure a proper copy.

Quote:
 Original post by HermesWhy can't i make it like this:Derived1(int t):y(t)/*,::Base1(t)*/{...::Base1(t)} It says something about t redefinition..

Base class initialization always comes before datamember initialization. You have to move your ::Base1( t ) to before your y(t).

Quote:
 Original post by HermesThe thing about not compiling without the default constructor is not so true since i can compile this ,strange isn't it

No, that's not strange at all. In fact, that's exactly what I told you would happen in my previous reply.

Quote:
 Original post by Polymorphic OOPRemember that if you don't define any constructors for your object then a default constructor is automatically generated for your type (which just default constructs all of the bases and members). So your situation is an impossibility.

You didn't define any constructors so it creates a constructor that takes no arguments and just default initializes all of the datamembers.