Default constructor/destructor

C

ctick

When defining a clas and no constructor and destructor provided, compiler
generates both.

What're the need for this since they do nothing as to
constructing/destructing an obejct.

What's happening in constructor/destructor if they both are defaulted and
empty?

Thanks!
 
J

John Harrison

ctick said:
When defining a clas and no constructor and destructor provided, compiler
generates both.

What're the need for this since they do nothing as to
constructing/destructing an obejct.

In one object contains another object, the the constructors and destructors
must be called for the contained objects.

class X
{
X();
};

class Y
{
X x;
};

The generated constructor for Y will call the default constructor for X,
because every Y contains an X. Similarly for destructors.
What's happening in constructor/destructor if they both are defaulted and
empty?

That's exactly the same as the generated ones. Default constructors and
destructors will be called for all contained objects.

john
 
C

cyper

ctick said:
When defining a clas and no constructor and destructor provided, compiler
generates both.

What're the need for this since they do nothing as to
constructing/destructing an obejct.
no,if u need a stack and u define a pointer, u must alloc memory for it.
and when to free it?
in the destructor,test if that pointer is not NUll,and free it.


What's happening in constructor/destructor if they both are defaulted and
empty?
they will do they should do.but no more
 
S

Sharad Kala

John Harrison said:
[snip]
class X
{
X();
};

class Y
{
X x;
};

The generated constructor for Y will call the default constructor for X,
because every Y contains an X. Similarly for destructors.

Ofcourse, you forgot to make constructor public or declare Y friend in X.
 
C

cyper

and when u pass an arg to a function
the function will call the constuctor of that object automaticlly
 
J

JKop

ctick posted:
When defining a clas and no constructor and destructor provided, compiler
generates both.

What're the need for this since they do nothing as to
constructing/destructing an obejct.

What's happening in constructor/destructor if they both are defaulted and
empty?


Absolutely nothing:


class SomeClass
{
public:

SomeClass(void)
{
;
}

};


-JKop
 
N

New_user

ctick said:
When defining a clas and no constructor and destructor provided, compiler
generates both.

Wrong! If you do not define you class constructor explicitly, compiler
will generate constructor/destructor only in case your class have
NON-TRIVIAL CONSTRUCTOR/DESTRUCTOR, eg:

0. Your class has virtual member-functions - implicitly generated
default ctor (and copy ctor) will setup vptr for your class

1. Has members with non-trivial constructors - implicitly generated
ctor will call them

2. Your class has base(s) with non-trivial ctor(s) - the same.

etc.
 
S

SaltPeter

ctick said:
When defining a clas and no constructor and destructor provided, compiler
generates both.

What're the need for this since they do nothing as to
constructing/destructing an obejct.

What's happening in constructor/destructor if they both are defaulted and
empty?

Thanks!

The answer is that without a constructor and destructor, compiler generated
or not, you can't create and/or destroy an instance of a class. Don't be
fooled by other languages that don't expose the existance of cstors and
d~stors. They certainly need them as well. The difference here is that you
have the option to define how your instance is initialized / constructed and
destroyed.

While the compiler generated constructor might fit the bill, you always have
the option of taking control in the case the default constructor doesn't
fullfill your needs. Consider:

class A
{
int m_number; // private variable
public:
A(n) : m_number(n) { } // cstor
~A() { } // d~stor
};

Note that i can now keep the m_number variable encapsulated without the need
to provide a member function to initialize it. The private variable is
initialized when the cstor is invoked with a default of 5 unless otherwise
specified.

#include <iostream>

class A
{
int m_number;
public:
A(int n = 5) : m_number(n) { }
~A() { }
void display() const { std::cout << "number is " << m_number <<
std::endl; }
};

int main()
{
A a;
a.display();

A aa(10);
aa.display();

return 0;
}

number is 5
number is 10
 
R

Robbie Hatley

cyper said:
when u pass an arg to a function the function will call
the constuctor of that object automaticlly

Not necessarily. Depends on how you pass the arg:
by value, by reference, or by pointer:

#include <iostream>

struct splat {char broiled;};

void Func1(splat par) {std::cout << par.broiled << std::endl;}
void Func2(const splat& par) {std::cout << par.broiled << std::endl;}
void Func3(splat* par) {std::cout << par->broiled << std::endl;}

int main(void)
{
splat blat; // Calls splat's implicit default constructor
blat.broiled = 'a';
Func1(blat); // Calls splat's implicit copy constructor
Func2(blat); // Does NOT call any constructors
Func3(&blat); // Does NOT call any constructors
return 0;
}

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top