?
Solved

C++ : constructor ....

Posted on 2003-10-27
3
Medium Priority
?
285 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

649 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