Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default constructor/destructor

Reply
Thread Tools

Default constructor/destructor

 
 
ctick
Guest
Posts: n/a
 
      06-24-2004
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!


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-24-2004

"ctick" <(E-Mail Removed)> wrote in message
news:dMuCc.126002$(E-Mail Removed)...
> 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.

>
> Thanks!
>


john


 
Reply With Quote
 
 
 
 
cyper
Guest
Posts: n/a
 
      06-24-2004

"ctick" <(E-Mail Removed)> 写入消息
news:dMuCc.126002$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      06-24-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>

[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.


 
Reply With Quote
 
cyper
Guest
Posts: n/a
 
      06-24-2004
and when u pass an arg to a function
the function will call the constuctor of that object automaticlly

"ctick" <(E-Mail Removed)> 写入消息
news:dMuCc.126002$(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      06-24-2004
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
 
Reply With Quote
 
New_user
Guest
Posts: n/a
 
      06-24-2004
"ctick" <(E-Mail Removed)> wrote in message news:<dMuCc.126002$(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
SaltPeter
Guest
Posts: n/a
 
      06-24-2004

"ctick" <(E-Mail Removed)> wrote in message
news:dMuCc.126002$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      06-28-2004
"cyper" <(E-Mail Removed)> wrote:

> 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






----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is Default.aspx not present in the Default Content page? Gaetan ASP .Net 3 02-14-2006 07:24 PM
default.mspx or default.do micky ASP .Net 1 12-17-2005 12:42 AM
Default Domain Policy vs Default Domain Controller Policy Tyler Cobb MCSE 6 10-19-2005 09:36 PM
Default Mozilla isn't default! Help! M. FERRANTE Firefox 1 07-27-2005 09:07 AM
automatic default route propagation into RIP: default metric Ben Low Cisco 2 12-28-2003 03:57 AM



Advertisments