473,766 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3267

"ctick" <ct***@flare.co m> дÈëÏûÏ¢
news:dM******** ************@bg tnsc04-news.ops.worldn et.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.co m> wrote in message
news:dM******** ************@bg tnsc04-news.ops.worldn et.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.co m> дÈëÏûÏ¢
news:dM******** ************@bg tnsc04-news.ops.worldn et.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.co m> wrote in message news:<dM******* *************@b gtnsc04-news.ops.worldn et.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.co m> wrote in message
news:dM******** ************@bg tnsc04-news.ops.worldn et.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**********@s ohu.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
2855
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
4739
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
8
5735
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
5804
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 ValueStruct { double x ; double y ; ValueStruct() { x = y = double(0) ; } // error * } ;
4
9503
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
3548
by: Alex Vinokur | last post by:
Compiler Green Hills C++, Version 4.0.6 --- foo.cpp --- struct A { }; struct B { B() {}
3
7374
by: Ganesh Rajaraman | last post by:
Hi, This is the program that i am trying. class A { public: A() { cout<<"Default Constructor"<<endl;
43
3826
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 "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
10
2434
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 virtual function; 2. virtual inheritance; 3.base class with explicit default constructor;
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.