Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a class object in c++

Tags:

c++

class

First i am from JAVA.

In java we create class object like this.

Example example=new Example(); 

The Example class can have constructor or cannot have constructor.

I can use the same in c++ like this

Example* example=new Example(); 

Where constructor is compulsory.

From this tutorial http://www.cplusplus.com/doc/tutorial/classes/

I got that we can create object like this.

Example example; 

Which do not require an constructor.

I have two questions.

  1. What is the difference between both the way of creating class objects.

  2. If I am creating object like Example example; how to use that in an singleton class.

like I usually do like this.

Sample* Singleton::get_sample() {     if (sample == NULL) {         sample = new Sample();     }     return sample; } 

Please guide me if I am wrong.

like image 669
Kathick Avatar asked Mar 09 '13 12:03

Kathick


People also ask

What is class object in C?

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and methods within a class are called members of the class.

How do you create a class object in C++?

Create a Class A class is defined in C++ using keyword class followed by the name of the class. The body of the class is defined inside the curly brackets and terminated by a semicolon at the end. class className { // some data // some functions };


2 Answers

I can use the same in c++ like this [...] Where constructor is compulsory. From this tutorial I got that we can create object like this [...] Which do not require an constructor.

This is wrong. A constructor must exist in order to create an object. The constructor could be defined implicitly by the compiler under some conditions if you do not provide any, but eventually the constructor must be there if you want an object to be instantiated. In fact, the lifetime of an object is defined to begin when the constructor routine returns.

From Paragraph 3.8/1 of the C++11 Standard:

[...] The lifetime of an object of type T begins when:

— storage with the proper alignment and size for type T is obtained, and

— if the object has non-trivial initialization, its initialization is complete.

Therefore, a constructor must be present.

1) What is the difference between both the way of creating class objects.

When you instantiate object with automatic storage duration, like this (where X is some class):

X x; 

You are creating an object which will be automatically destroyed when it goes out of scope. On the other hand, when you do:

X* x = new X(); 

You are creating an object dynamically and you are binding its address to a pointer. This way, the object you created will not be destroyed when your x pointer goes out of scope.

In Modern C++, this is regarded as a dubious programming practice: although pointers are important because they allow realizing reference semantics, raw pointers are bad because they could result in memory leaks (objects outliving all of their pointers and never getting destroyed) or in dangling pointers (pointers outliving the object they point to, potentially causing Undefined Behavior when dereferenced).

In fact, when creating an object with new, you always have to remember destroying it with delete:

delete x; 

If you need reference semantics and are forced to use pointers, in C++11 you should consider using smart pointers instead:

std::shared_ptr<X> x = std::make_shared<X>(); 

Smart pointers take care of memory management issues, which is what gives you headache with raw pointers. Smart pointers are, in fact, almost the same as Java or C# object references. The "almost" is necessary because the programmer must take care of not introducing cyclic dependencies through owning smart pointers.

2) If i am creating object like Example example; how to use that in an singleton class.

You could do something like this (simplified code):

struct Example {     static Example& instance()     {         static Example example;         return example;     }   private:      Example() { }     Example(Example const&) = delete;     Example(Example&&) = delete;     Example& operator = (Example const&) = delete;     Example& operator = (Example&&) = delete;  }; 
like image 65
Andy Prowl Avatar answered Sep 23 '22 13:09

Andy Prowl


Example example; 

This is a declaration of a variable named example of type Example. This will default-initialize the object which involves calling its default constructor. The object will have automatic storage duration which means that it will be destroyed when it goes out of scope.

Example* example; 

This is a declaration of a variable named example which is a pointer to an Example. In this case, default-initialization leaves it uninitialized - the pointer is pointing nowhere in particular. There is no Example object here. The pointer object has automatic storage duration.

Example* example = new Example(); 

This is a declaration of a variable named example which is a pointer to an Example. This pointer object, as above, has automatic storage duration. It is then initialized with the result of new Example();. This new expression creates an Example object with dynamic storage duration and then returns a pointer to it. So the example pointer is now pointing to that dynamically allocated object. The Example object is value-initialized which will call a user-provided constructor if there is one or otherwise initialise all members to 0.

Example* example = new Example; 

This is similar to the previous line. The difference is that the Example object is default-initialized, which will call the default constructor of Example (or leave it uninitialized if it is not of class type).

A dynamically allocated object must be deleted (probably with delete example;).

like image 21
Joseph Mansfield Avatar answered Sep 25 '22 13:09

Joseph Mansfield