The daily ramblings …

C++ : Methods of Object Instantiations with Classes

Posted in C++ General by akamal on January 16, 2007

When it comes to programming, the best method to learn is by digging up codes from existing source codes, but question remains, how do you learn by looking at thousands of lines of codes? Well firstly, you have to get your basics right.

To recap, Classes and OOP are the main feature provided by C++. With classes, you’d be able to expand your built codes for other future needs, or simply to tackle similar problems by re usability. The purpose of this article is not to look at classes (well not now), but rather a quick peek on the styles of code-writing when instantiating / using classes. Remember that the concept of class is strongly linked to pointers, especially in C++. So you might want to get a brush-up on it first. Look it up here.

Fundamentally, there are 2 styles when writing class instantiations.

  1. The first style assigns a variable with the instantiation of the object itself (as seen in type A).
  2. The second, goes for a more versatile approach by using a variable to store the address of the created object (type B).

type B:

1// We assign a variable named window in line 9. 
2// Its type is not int, char or whatever, but instead it is an 
3// object of class QWidget.
4// To access its properties, we use "." to access its method,
5// as seen in line 10 ---
6int main(int argc, char * argv[])
8	QApplication app(argc,argv);
9	QWidget window;
11	return app.exec();

type B:

1// here, we initially declare a pointer named window 
2// which is of type object. Variable window be allocated in the 
3// memory with no value. Next, we assign the window variable to the 
4// address of the QWidget object just created (new QWidget creates an object).
5// To access its properties, we use "->", known as throwing at, 
6// to access method show() as seen in line 6.
7int main (int argc, char * argv[])
9QApplication app(argc,argv);
10QWidget *window;
11window = new QWidget;
13return app.exec();

Between the 2, there’s aren’t many differences generally, and certainly no pros/cons that I know of. But just to get some people accustomed to the style of writing, one should be aware that class instantiation can be done in 2 ways, as explained above.


3 Responses

Subscribe to comments with RSS.

  1. Azlan said, on January 16, 2007 at 8:59 am

    Hey, nice intro into the programming world. Hehe always a c++ addict huh? So, Kamal, still on software development? Last I heard you were in physical science or something.

    How’s life been?

  2. Ur Wife said, on January 16, 2007 at 1:21 pm

    Hye Dear,
    What a nice blog u have.
    I was thinking it will be more interesting blog if u include more pictures of us and description about me (as ur wife) not forgetting our marriage life, dear.

    Anyway keep it up.!!!!

  3. Soyuz said, on June 5, 2008 at 2:18 am

    These 2 styles makes BIG difference. I am not going to write in detail but will give you a pointer to explore:

    In your example, the former style creates the object in stack and the later style creates the object in heap. For the first case, the destructor is called when object goes out of scope but for the later it’s not. etc.

    Dig on .. its a must know for a C++ programmer

    But its true, for the “given” example here, we can neglect the difference or pro/cons.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: