that's it

what's the relation between constructor and creating an object?

many thanks.

Recommended Answers

All 6 Replies

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.

it helped so much ..

thanx .

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.

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 ?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.