Is there a way to do something like this? (initialising pointers)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class someclass{
private:
int x;

public:
someclass(int x1);
}; 

someclass::someclass(int x1):x(x1){
}

int main(){
int x;
someclass *a;

if( x < 0) a = new someclass(2);
else a = new someclass(1);


return 0;
}


Basically i want to be able to declare the pointer and then initialise it later to save having to declare multiple pointers.

Being able to do it similar to this would make my code so much cleaner.

Cheers for any help.
Last edited on
I don't fully understand your question. You seem to have answered it yourself. What do you want to do that your code does not do?
I was just fairly sure that wouldn't compile. I'll go check now sorry to waster your time if so!:)
Sick yes it does sorry to have wasted your time.
Topic archived. No new replies allowed.