Solved

C++ : constructor ....

Posted on 2003-10-27
3
275 Views
Last Modified: 2010-04-17
Hi C++ Experts,

   I have a question about constructors : When we call the constructor, it allocates memory for an object,
does it also initialize the members of the object ? Also,
is the internal representation of the object in the memory contiguous ? is the memory allocated in the heap or in the stack ? Thanks a lot !!!

meow .............
0
Comment
Question by:meow00
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 9627582
Well, let's start at the end.


Is memory allocated on the heap or the stack?

That depends. If you have an object MyObj, and you write:

MyObj m=new MyObj();

Then the memory is allocated from the heap. However, if you write:

MyObj m();

it is allocated on the stack.


Is the internal representation of the memory contiguous?

Yes.


Last question in two parts:

When we call the constructor, it allocates memory for an object....

Actually no. The constructor does not allocate memory. That has already been done either by the 'new' call, or implicitly on the stack.

does it also initialize the members of the object?

Maybe. An implicit constructor doesn't do much of anything. If there are piece of the object that require initialization, you should write a constructor that does that. Initialization, certainly, is the main point of having a constructor.


0
 
LVL 16

Expert Comment

by:imladris
ID: 9627590
P.S. you should note that it is usually better to ask C++ questions in the C++ topic area.
0
 

Expert Comment

by:Scarecrow248
ID: 9630938
 Hi Mewoo!
  In my opinion i think you're right that "When we call the constructor, it allocates memory for an object"
  when you say:  Sport Football;
  a constructor Sport() is call and this constructor allocate memory for the object Football.
  Remember that whenever you declare a contructor,declare a destructor(although it's true that the default destructor would work well). As constructors create and initialize objects of your class,destructors clean up after your object and free any memory you've allocated.
  The default constructor(the compiler make one for you if you don't write yourself) takes no argument. But you can write some constrcutions yourself and the main pont is to initialize member data of class.
  Here is an example how to do it:
  class Sport
{
   public:
           Sport(){}  //constructor that takes no argument
           Sport(int nbr):KindSport(nbr){} //constructor that takes 1 argument and is used to initialize KindSport
           ~Sport(){}//destructor
   private:
           int KindSport;
};
 Hope it helps

   
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 102
Not needed 13 113
Removing line numbers from left column in config file 7 53
How to Convert a Quote to an Order in SalesForce 2 43
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question