Solved

C++ : constructor ....

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is the compatible hibernate version for spring 4.3.1 10 64
hasOne  challenge 59 92
groovy example issue 10 70
recursion example 16 70
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now