Creating a class object in c++

前端 未结 8 2170
离开以前
离开以前 2020-12-07 11:08

First i am from JAVA.

In java we create class object like this.

Example example=new Example();

The Example class can have constructor

相关标签:
8条回答
  • 2020-12-07 11:49

    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;
    
    };
    
    0 讨论(0)
  • 2020-12-07 11:50

    First of all, both cases calls a constructor. If you write

    Example *example = new Example();
    

    then you are creating an object, call the constructor and retrieve a pointer to it.

    If you write

    Example example;
    

    The only difference is that you are getting the object and not a pointer to it. The constructor called in this case is the same as above, the default (no argument) constructor.

    As for the singleton question, you must simple invoke your static method by writing:

    Example *e = Singleton::getExample();
    
    0 讨论(0)
  • 2020-12-07 11:50

    In the first case you are creating the object on the heap using new. In the second case you are creating the object on the stack, so it will be disposed of when going out of scope. In C++ you'll need to delete objects on the heapexplicitly using delete when you don't Need them anymore.

    To call a static method from a class, do

    Singleton* singleton = Singleton::get_sample();
    

    in your main-function or wherever.

    0 讨论(0)
  • 2020-12-07 11:52
    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;).

    0 讨论(0)
  • 2020-12-07 11:53

    There is two ways to make/create object in c++.

    First one is :

    MyClass myclass; // if you don;t need to call rather than default constructor    
    MyClass myclass(12); // if you need to call constructor with parameters
    

    Second one is :

    MyClass *myclass = new MyClass();// if you don;t need to call rather than default constructor
    MyClass *myclass = new MyClass(12);// if you need to call constructor with parameters
    

    In c++ if you use new keyword, object will be stored in heap. it;s very useful if you are using this object long time of period and if you use first method, it will be stored in stack. it can be used only short time period. Notice : if you use new keyword, remember it will return pointer value. you should declare name with *. If you use second method, it doesn;t delete object in the heap. you must delete by yourself using delete keyword;

    delete myclass;
    
    0 讨论(0)
  • 2020-12-07 11:56

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

    a) pointer

    Example* example=new Example();
    // you get a pointer, and when you finish it use, you have to delete it:
    
    delete example;
    

    b) Simple declaration

    Example example;
    

    you get a variable, not a pointer, and it will be destroyed out of scope it was declared.

    2) Singleton C++

    This SO question may helps you

    0 讨论(0)
提交回复
热议问题