Can I declare an object variable without initializing it? This is mostly a scope problem:

Code:

int main () {
SomeObj eg; // but this is an initialization ...
try {
eg(blah blah); // so this is pointless
}

If I move the declaration into the try block, it is not in main's scope. I know I can do this:

Code:

int main () {
SomeObj *eg;
try {
eg = new SomeObj(blah blah);
}

But now I am using a pointer when I don't need to; also it's my understanding that if you use new, you then need to explicitly use delete.

Kind of a hassle, is there any way out?

03-20-2010

grumpy

A declaration of a variable of class type will always initialise the object by invoking a class constructor. If that initialisation fails, an exception will be (or should be) thrown. An exception causes the stack to be unwound - until a scope is reached where, effectively, that object has never existed.

Code:

int main()
{
SomeObj example;

example.something(); // this line will not be reached if construction of example fails
}

If you're worried about example.something(); being called if construction of example fails, then don't. The standard guarantees it will not happen - assuming the constructor throws an exception if the object cannot be constructed in a valid state.

03-20-2010

C_ntua

Code:

int main () {
SomeObj* egPtr;.
try {

SomeObj eg(blah blah);
egPtr = &eg;
}

Can't you do sth like the above? You initialize the object where you want it to be initialized and you use a pointer for it to be in the scope you want it.

You are not using a pointer when you don't need to. Instead of using eg, you use egPtr. Meaning that outside the try only egPtr is available.

EDIT: Sorry was in a hurry. The above obviously won't work...

03-20-2010

mustermeister

An object will always be initialized when you declare it. Why do you want to avoid that? More info please.

03-20-2010

MK27

I want to avoid it so I can do a try/catch on the initialization, without having to resort to a pointer for a local variable.

03-20-2010

grumpy

Since a variable declared in a scope ceases to exist outside that scope, this can do what you want

More broadly, good practice with exceptions, usually involves minimising the number of try/catch blocks. Having one for a single operation tends to make the software, as a whole, hard to maintain.

03-20-2010

VirtualAce

Quote:

SomeObj *eg;

Even in this instance I would prefer to initialize the pointer to 0.

03-20-2010

mustermeister

Quote:

Originally Posted by MK27

I want to avoid it so I can do a try/catch on the initialization, without having to resort to a pointer for a local variable.

So put the declaration inside the try-catch block. Use grumpy's code.

03-20-2010

King Mir

Quote:

Originally Posted by MK27

If I move the declaration into the try block, it is not in main's scope. I know I can do this:

Code:

int main () {
SomeObj *eg;
try {
eg = new SomeObj(blah blah);
}

But now I am using a pointer when I don't need to; also it's my understanding that if you use new, you then need to explicitly use delete.

You do need a pointer here, because if construction fails, you would not have a valid object to work with. So you need a pointer than can be set to null.

Better to use code like grumpy. Then whatever uses the object will not have to worry about dealing with an invalid object.

03-21-2010

VirtualAce

Quote:

Better to use code like grumpy. Then whatever uses the object will not have to worry about dealing with an invalid object

This can also be accomplished without the try catch block if you only do operations in a constructor that are guaranteed to never fail. What if if someone wanted to declare a SomeObj instance on the stack in a class?

Code:

class Foo
{
public:

private:
SomeObj m_someObj;
};

03-21-2010

grumpy

That's not what the question was about, Bubba. The question concerned being able to catch exceptions thrown when constructing an object within a try block, but allow that object to be used after the try block. He wanted to be able to do this;

server.cpp: In function ‘int main()’:
server.cpp:92: error: ‘valis’ was not declared in this scope

Is this some nuance here that will make this work? It seems very silly to me (altho it is normal scoping, wouldn't it be nice if try "returned" it's variables on sucess?), I am glad to hear it is not supposed to be that way.

03-21-2010

laserlight

If you read grumpy's example in post #6, you should be able to see that this can work:

This can also be accomplished without the try catch block if you only do operations in a constructor that are guaranteed to never fail. What if if someone wanted to declare a SomeObj instance on the stack in a class?

Good point, and I had been thinking to just use an empty constructor* and then a separate init() function. But I don't think that should be necessary, and the reading I've done explicitly stated that part of the purpose of exceptions was for constructors, since constructors cannot return a value, making normal error checking awkward (tho there are other simple ways around that).

*It is hard to meaningfully "construct" an inet server object that does not involve some operations that cannot be guaranteed not to fail.

03-21-2010

MK27

Quote:

Originally Posted by laserlight

If you read grumpy's example in post #6, you should be able to see that this can work:

Well of course it will :p So am I suppose to keep all of main inside the try block then?