C++ easy question - can't I declare an Object inside an IF statement?

Bengt4

Member
Jan 12, 2001
68
0
0
Hi there, I do program in C for about 2 years, but only now I am doing some C++ coding, and using Classes, etc. I found C++ a LOT more powerful then C, but I am having a litle problem with THIS code (that is inside the main funcion, and with user input handling cut out):

//*****************

if (option)
{
cout << "1) Enter the year" << endl;
cin >> c_year;
cout << "2) Enter the chassis number" << endl;
cin >> c_chassis;

Car first_one = (c_year, c_chassis);
} // end of if (option)

//***********

then, in the end of the main funcion, when I try to cout any of the the first_one Car object values, the compiler (MS Visual C++ 6) gives me and error message that says: "first_one: undeclared identifier".
So my question is: can't I declare an Object inside an IF statement? Why not? Is the IF statement really a funcion and when it ends the first_car got out of scope? Or is it something else? PLEASE HELP =]
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
Any variables you declare in a {} block are only valid within that block.

So your code is invalid because first_one goes "out of scope" at the end of the if-statement and is no longer accessible. You have to declare the variable before the if-statement.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Car first_one = null; //this means that it will be available everywhere in this funciton
if (option)
{
cout << "1) Enter the year" << endl;
cin >> c_year;
cout << "2) Enter the chassis number" << endl;
cin >> c_chassis;

first_one = new Car(c_year, c_chassis); //if the if block happens, it gets set to non-null
} // end of if (option)

if(first_one == null)
{
//it didn't do that if above, so be careful about null pointers
}
else
{
//it did do that if, so it is safe to use the Car
}
 

Bengt4

Member
Jan 12, 2001
68
0
0
"Car first_one = null; //this means that it will be available everywhere in this funciton"

it also means that first_one was created, and using the default constructor, and the fist_one object pointer (this) is null? I have a "default" (does nothing) and an overloaded constructor. All I want is, in fact, create the object first_one ONLY IF the program requires. The only way to do it is with pointers isn't it?

Well I'll have to make a lot of cars, because I wanna design a basic add/delete/list database of cars, sorted by year, at first, only as an exercise to move on more complex stuff. I'll try do do it with an list. Wish me luck! =]

It's good to see I got help fast here. But I could see that this forum is about a lot of things, not C++ programming. Is there a FREE forum on the Internet for programming design or samething? A C++ forum...? The www.cplusplus.com forum is OK, but I didn't like it and I got no replyes yet from it.
 

MSantiago

Senior member
Aug 7, 2002
308
0
86
Both of them are correct. You need to declare first_one before entering the if block. If you declare first_one and assign it a value of null, no objects are being created; only a pointer. The only time a constructor is called is when you initialize an instance of the class by using new. If you've overloaded the constructor, the paramaters you pass into the new Car() statement will determine which one is called.
 

michaelh20

Senior member
Sep 4, 2000
482
0
0
>>The only time a constructor is called is when you initialize an instance of the class by using new.

Is this really true? I think you can just declare an object

MyObject mobj (x, y, x) ;

And really, every new allocates heap space and must be matched with delete -- quite unlike java...

Not that I quite know
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
I'll fix a little problem with CTho's code, he should create a pointer to first_one like this:

Car *first_one = NULL; //this means that it will be available everywhere in this funciton

instead of:

Car first_one = null; //this means that it will be available everywhere in this funciton

the rest of his code should be OK.


Originally posted by: CTho9305
Car first_one = null; //this means that it will be available everywhere in this funciton
if (option)
{
cout << "1) Enter the year" << endl;
cin >> c_year;
cout << "2) Enter the chassis number" << endl;
cin >> c_chassis;

first_one = new Car(c_year, c_chassis); //if the if block happens, it gets set to non-null
} // end of if (option)

if(first_one == null)
{
//it didn't do that if above, so be careful about null pointers
}
else
{
//it did do that if, so it is safe to use the Car
}

 

Turkey

Senior member
Jan 10, 2000
839
0
0
You could delay the declaration of the object until just before you need to use it, since c_year and c_chassis are both in main {} scope.

if (option) {
// get the data
}

... do some stuff ...

if (option) {
Car first_one(c_year, c_chassis);
... do what you need to do with first_one ...
}


This is probably the best way to do it. Assuming your compiler doesn't allocate resources for the object way before they are actually needed, this will use the Car resources for the shortest amount of time. It's also cleaner IMO, because declaration and initialization occur in one statement instead of two statements that are many lines of code apart. This will also prevent you from needing to remember to call delete.