473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Default constructor/destructor

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!
Jul 22 '05 #1
8 3244

"ctick" <ct***@flare.com> дÈëÏûÏ¢
news:dM********************@bgtnsc04-news.ops.worldnet.att.net...
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

Jul 22 '05 #2

"ctick" <ct***@flare.com> wrote in message
news:dM********************@bgtnsc04-news.ops.worldnet.att.net...
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
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2j*************@uni-berlin.de...
[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.
Jul 22 '05 #4
and when u pass an arg to a function
the function will call the constuctor of that object automaticlly

"ctick" <ct***@flare.com> дÈëÏûÏ¢
news:dM********************@bgtnsc04-news.ops.worldnet.att.net...
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!

Jul 22 '05 #5
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
Jul 22 '05 #6
"ctick" <ct***@flare.com> wrote in message news:<dM********************@bgtnsc04-news.ops.worldnet.att.net>...
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.
Jul 22 '05 #7

"ctick" <ct***@flare.com> wrote in message
news:dM********************@bgtnsc04-news.ops.worldnet.att.net...
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

Jul 22 '05 #8
"cyper" <am**********@sohu.com> 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 =---
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Morris | last post by:
I have a question. How many functions are defined by default when you define a class. I was aware of four functions: Constructor Destructor Assignment Operator Copy Contructor
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
8
by: meendar | last post by:
what will a object of an Empty class( contain nothing), do on default.What are all the default methods it calls. what is the use of creating the object for an empty class?
9
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct...
4
by: moleskyca1 | last post by:
Hi, In a recent discussion, some of us were in disagreement about the functions the C++ compiler generates. How many functions are generated by the compiler when you declare: class Foo { };...
9
by: Alex Vinokur | last post by:
Compiler Green Hills C++, Version 4.0.6 --- foo.cpp --- struct A { }; struct B { B() {}
3
by: Ganesh Rajaraman | last post by:
Hi, This is the program that i am trying. class A { public: A() { cout<<"Default Constructor"<<endl;
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.