Solved

C++ : constructor ....

Posted on 2003-10-27
3
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
Simple Linear Regression

726 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