instanciate object

Jan 10, 2011 at 8:57pm
hey everybody , plz help me on that , i wanna make something like that
1
2
3
cout<<"do u wanna create an object ?"<<endl;
cin>>a
if(a==1) { ??????????????????????? }

what should i make between { } ???
Jan 10, 2011 at 9:10pm
Jan 10, 2011 at 9:13pm
1
2
3
cout<<"do u wanna create an object ?"<<endl;
cin>>a
if(a==1) { ??????????????????????? }


You missed a semicolon there at the end of the second line, and that big stack of question marks doesn't look right. What exactly are you trying to do? If you want to create an object in there, how about this?

1
2
3
cout<<"do u wanna create an object ?"<<endl;
cin>>a;
if(a==1) { int x; }


That will create an int object (which will be deallocated very shortly afterwards, but if all you want to do is create an object, that will do it.
Jan 10, 2011 at 9:17pm
you didn't understand my question , srry for my bad english :D
here is the source code :
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
class A 
          {...............................};
int main()
          {    cout<<"do u wanna create an object ?"<<endl;
              cin>>a;
              if(a==1) { i don't know what to do here  }
           } 


i know about dynamic allocation , but that's not what i want :)
Jan 10, 2011 at 9:30pm
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
class A 
          {int eggs;};
int main()
          {    cout<<"do u wanna create an object ?"<<endl;
               int a;
              cin>>a;
              if(a==1) {A aNewInstanceOfObjectTypeA; cout << "Done";  }
              return 0;
           } 

This code compiles and runs; an object of your class A is instantiated there, on line 9.

Do you want the object to exist beyond the scope of the 'if' statement?
Last edited on Jan 10, 2011 at 9:33pm
Topic archived. No new replies allowed.