3
Contributors
6
Replies
8
Views
10 Years
Discussion Span
Last Post by ~s.o.s~
0

Objects are created when you declare them (when they are on the stack frame) or when they are dynamically allocated.

// object declared and created, mem allocated, default constructor called
Object obj ; 

// object created dynamically ,default constructor called 
Object o = new Object( ) ;

The way constructors fit in the picture is when you want to assign values the class members or variables at the time of creation which you cant do without a constructor.

Hope it helped, bye.

0

Nope. The constructor is the method that is called when the object is initialized. You can have multiple constructors, each with different signatures.

The object is instantiated when you "create" it. After instantiation comes initiliazation, which is the job of the constructor.

0

Objects are created when you declare them (when they are on the stack frame) or when they are dynamically allocated.

Nope.

The above sentence is in conformance with the "Professional C++" by Wrox Press, you dont agree with this ?

If a class has a constructor, the compiler automatically calls that constructor at the point an object is created

class X {
int i;
public:
X(); // Constructor
};
Now, when an object is defined,
void f() {
X a;     // here a is *defined*, X obj created and constructor is called
}

Thats what I said in my previous post, what is wrong about it ?

0

I agree with t. But the constructor doesn't do the creating. It does the initializing. Since the poster asked for a more "specific answer" (she since edited that out of her post), I clarified.

I wasn't arguing with you, but object instantiation, and object initialization, are separate (but related) things. The "constructor" is called during initialization.

0

Since the poster asked for a more "specific answer" (she since edited that out of her post), I clarified.

That explains it all ;)

I wasn't arguing with you, but object instantiation, and object initialization, are separate (but related) things. The "constructor" is called during initialization.

Neither was I but your stmts got me confused so I just wanted to get things clarified.

Peace all.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.