Solved

C++ : constructor ....

Posted on 2003-10-27
3
280 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

Technology Partners: 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

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

730 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