Is there a way to call constructor with class instance pointer?
We can call destructor explicitly through class pointer, why not constructor? Any idea?
#include <iostream>
class Con {
public:
    Con( int 开发者_JS百科x ) : x( x ) {
    }
private:
    int x;
};
int main() {
    Con* c = new Con( 1 );
    //c->Con( 2 ); //illegal
    c->~Con(); // ok!
    delete c;
}
Thanks,
You can actually call it, it is just that the syntax is not that of calling a member method (of which the destructor is an special case), so it is not done with the member access operators. Rather you have to resort to the placement-new syntax:
Con c;
c.~Con();        // destroy, now c is not a Con anymore
new (&c) Con();  // recreate, now c is a Con again
As a particular case, in the C++0x proposal, that is actually used in one of the code examples, providing means to reuse a union as a different type in the event of an union containing non-POD elements:
union U {
   int i;
   float f;
   std::string s;
};
int main() {
   U u;
   new (&u.s) std::string( "foo" );
   u.s.~string();
   u.i = 5;
}
}
No. You cannot.
Con* c = new Con( 1 );
//c->Con( 2 ); //illegal
You've already called the constructor in the new expression. 
By the time you've a valid pointer of type Con*, you've already created an object. And calling constructor on the "constructed" object doesn't even make sense. So why would C++ allow that? 
It will be easier for you if you don't think of constructor and destructor as a functions, that you call. You don't call them. You can only construct or destruct an object. And, as a part of constructing, constructor body is executed. Same, as a part of object destruction, destructor body is executed.
So you can construct object on the stack
YourClass variable(constructor_arguments);
and it will be destructed automatically when it's out of scope.
You can also create object on the heap
YourClass * ptr = new YourClass(parameters);
To destruct such an object you use operator delete
delete ptr;
You can also construct an object in some memory you provided by yourself (rarely needed)
char * pool = new char[sizeof(YourClass)]
YourClass *ptr = new(pool) YourClass(parameters);
You destruct such an object explicitely and the syntax looks like function invokation, but it's rather an object destruction
ptr->~YourClass();
After this line your object is no more. Invoking anything on it is an undefined behavior. And you still have to manage the memory you allocated for this object
delete[] pool;
So, your question means 'Why can I explicitely destruct an object to which I have a pointer but I can't construct it'? You can't, because it is already constructed.
You can also read C++ FAQ Lite explanation
You can only call the constructor when the object is being constructed, hence it's name. Once the object is constructed, I see no reason why you'd want to call it again on the same object. If you want to do something then, you need to call a function defined in that class.
A Constructor's intent is to be called when the object is created. Nothing else. If you had a reference counter to keep track of the number of objects, allowing the constructor to be called as a function would mess the counter up.
If you want to reinitialize or reset an object, you can add a function called Reset() or Initialize() and call it from the constructor. Then, you can also call Reset() or Initialize() from an object pointer.
No you cant call the constructor of a class the way you explained, and that's because, c doesn't point to a valid object of con type.
Ofc you can call it from an object instance , see this code :
 #include <iostream>
class foo{
      public :
      int foo_var;
      foo(){
            std::cout<<"foo_var = "<<foo_var<<std::endl;
            }
      };
int main()
{
    foo * f = new foo();
    // set foo_var
    f->foo_var = 10;
    // call constructor by passing object instanse
    foo * f1 = new(f) foo;
    std::cout<<(f==f1)<<std::endl;
    system("pause");
    return 0;
}
the second output is "foo_var = 10" so it is working . the third output is "true" then (f==f1) that mean the constructor does not allocate new memory (the operator new does , but we passed a pointer to it so he wont alloc an other) there you are a really usefull practice :
#include <iostream>
template <class t>
class smart_pointer{
      t * p; // the real normal pointer
      public :
      smart_pointer()
      {
          p = (t*) malloc(sizeof(t)); // allocate memory 
          try{ 
          new (p) t; // call the constructor 
          } catch (...)  // if it throws any exception
          {
                  free(p); // free p , dont use delete , because it will
                  // call the destroctor calling the destructor will cause 
                  // freeing a not allocated memory that causes a crash
                  throw; // throw the exception what ever it was
          }
      }
      ~smart_pointer(){delete p;}
      t operator = (t val) // assigment operator
      {
               *p = val;
               return val;
      }
      operator t(){ // type casting operator , usually for std::cout
               return *p;
               }
      };
int main()
{
    smart_pointer<int> x;
    x = 10;
    std::cout<<x<<std::endl;
    system("pause");
    return 0;
}
it is recommended to read "effective c++" , "more effective c++" , "effective modern c++" books
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论