tags:

views:

104

answers:

6

I have a class that has only one function "Print()" and two properties "age, sex". And i am trying to understand when is object creating on memory?

i can always access to object's props and functions even i didn't call it's constructor function.

Isn't there any rule for the creating object from the class?

In C# this won't create object on memory: ClassName cls; But this will create: ClassName cls = new ClassName(); In C++ is there any way to not create object in memory until i need to call it's constructor function?

#include <QtCore/QCoreApplication>
#include "iostream"

using namespace std;

class ClassName{
public:
    void print(){
        cout<< "Age: " <<age <<endl;
        cout<< "Sex: " <<sex <<endl;
    }
    int age;
    char sex;


};


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    ClassName *ex1 = new ClassName();
    ex1->print();

    ClassName ex2;
    ex2.print();

    ClassName ex3= {10,'e'};
    ex3.print();

    ClassName exCopy(ex3);
    exCopy.print();

    return a.exec();
}
+4  A: 

I'm not sure what you are asking, exactly, but hopefully the following exposition contains the answer you are looking for.

Variables ex1, ex2, ex3 and exCopy are all associated with distinct instantiations of ClassName.

  1. ex1 points to a heap-allocated instance. Since you never invoke delete on it, the instance leaks on exit from the program, which doesn't matter much in this case, but might cause problems if ClassName had a non-trivial destructor that had to be called before exit.
  2. ex2 is a default-initialised stack instance, which will have, for all intents an purposes, random values in age and sex.
  3. ex3 is a member-initialised stack instance.
  4. exCopy is a copy-constructed stack instance.
Marcelo Cantos
+1  A: 

Perhaps you might want to read about constructors http://www.parashift.com/c++-faq-lite/ctors.html and copy constructors http://www.cplusplus.com/articles/jsmith1/

LumpN
+2  A: 

In C++ is there any way to not create object in memory until i need to call it's constructor function?

Yes - create it dynamically:

ClassName * p = 0;   // nothing created
...                  // wait a bit
p = new ClassName;   // create instance
...                  // wait a bit
delete p;            // get rid of it
anon
+2  A: 

In C# this willn't create object on memory: ClassName cls; But this will create: ClassName cls = new ClassName(); In C++ is there any way to not create object in memory until i need to call it's constructor function?

The equivalent to your C# example, using C++, is as follows:

//pointer to ClassName, not initialized
ClassName* cls1;
//alternatively
ClassName* cls2 = 0;
//initialize after declaring it but before using it
cls1 = new ClassName();
cls1->print();
//after initializing it, don't forget to delete it again later
delete cls1;
cls1 = 0;
//this is an error (using uninitialized pointer)
cls2->print();

C# classes are 'reference' types, which are like C++ 'pointer' types.

ChrisW
This is very good point: "C# classes are 'reference' types, which are like C++ 'pointer' types."
uzay95
-1: This is actually not the equivalent to a C# reference. A C# reference is managed by the garbage collector, which is not the case here. The application has to manage the memory on it's own (as @Neils answer points out).
Space_C0wb0y
@Space_C0wb0y You're right: they're like pointer types in many ways(e.g. they can be null and need to be initialized), but not in all ways (eg. they're garbage-collected whereas C++ pointers aren't).
ChrisW
A: 

So what you re asking is: "How can I create an unbound reference in C++?" - Well, you can do that only with pointers, either a plain pointer, or a smart pointer like Boosts shared_ptr, which should be preferred.

{
    tr1::shared_ptr<ClassName> p; // p is a NULL-pointer

    p.reset(new ClassName()); // now a new object is created on the heap and p points to it.
} // no delete needed, shared_ptr counts references and deletes object when no more reference exis
Space_C0wb0y
A: 

In C++ is there any way to not create object in memory until i need to call it's constructor function?

It's possible to allocate the memory first and then initialize the object later. Is that what you mean?

unsigned char memory[sizeof(ClassName)];

// ...

ClassName* p = new(memory) ClassName();
p->print();

// ...

p->~ClassName();

(Although I'm pretty sure this is not what you want.)

FredOverflow
This is also very good information for beginners like me. Thank you for your kind and very good answer.
uzay95
Note that unless you reinvent `std::vector`, it's pretty unlikely that you'll ever need to know this kind of stuff.
FredOverflow