2

I'm experimenting on classes and constructors and i'm trying to choose a specific declaration for a class object depending on something like an if-statement. I've written a simple example showing what i tried to do, which is not working. Even though it fulfills the if-statement, it prints the "id" of the first declared object and if i don't declare it before the if statement i get the error "a not declared in this scope" for the print. Is there a way to re-declare a class object and use it afterward through an if-statement?

class potato{

    private:
     int id;
    public:

    void get_id(){
        cout<<id<<endl;
    }

    potato(int x){
        id=x;   
    }

};

int main(){

    int i=9;
    potato a(i);
    if(i==3){
        potato a(5);
    }
    else
        potato a(3);


    a.get_id();


}
Alex Matt
  • 209
  • 1
  • 4
  • 4
    You seem to have quite a few misunderstandings about C++. You should take a step back and systematically learn the language from a good book. – Baum mit Augen May 06 '18 at 18:05
  • ^ agreed. In this specific case, you don't have to re-initialize `a` to change the value of the `id` class member. You can just create a class function like `void set_id(int _id) { id = _id }` and call it in your if-else scope. – Ali250 May 06 '18 at 18:07
  • 2
    @BaummitAugen Comment best augmented with [link to SO list of good C++ books](https://stackoverflow.com/q/388242/1782465). – Angew is no longer proud of SO May 06 '18 at 18:20
  • You need to learn the differences between declaring something (telling the compiler it exists) and the definition of that thing (telling the compiler how it's actually *made*), and the difference between a class, and an instance of that class (called an "object"). And that's all just from the title of this question. – Steve May 06 '18 at 18:20
  • It's important to understand these details, and once you understand those, you're well on your way to understanding what's wrong in your program. – Steve May 06 '18 at 18:22
  • `potato a( i == 3 ? 5 : 3 );` – M.M May 06 '18 at 21:40

2 Answers2

4

The a objects in the if-else blocks are different objects than the one before them. They are created and destructed in the if-else block and don't change the first object.

potato a(i);  // Object 1
if(i==3){
    potato a(5);  // Object 2. 
}
else
    potato a(3); // Object 3


a.get_id(); // Still object 1

If you want to change the first object, use assignment.

potato a(i);  // Object 1
if(i==3){
    a = potato(5);  // Changes object 1. 
}
else
    a = potato(3); // Changes object 1. 


a.get_id(); // Should have new value
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Is there a way to re-declare a class object

Yes. You can declare an object many times. But only define it once (ODR - the One Definition Rule).

and use it afterward through an if-statement? [sic - grammar error]

  • An object "in scope" can be 'used'.

  • Any object out-of-scope can not.

In main, you have 3 scopes, which overlap. Here is your code in which I added braces to clarify the scopes.

int main(int, char**)
{                       // scope 1 begin

    int i=9;
    potato a(i);        // scope 1 potato ctor
    if(i==3)           
      {                    // scope 2 begin
          potato a(5); 
          a.show();        // of potato in scope 2 (scope 1 potato is hidden)
      }                    // scope 2 end

    else  

      {                    // scope 3 begin
          potato a(3);
          a.show();        // of potato in scope 3 (scope 1 potato is hidden) 
      }                    // scope 3 end

    a.get_id();
    a.show();         // show contents of scope 1

}                     // scope 1 end

You may use the scope 1 potato at every scope if you do not hide or shadow the object:

int main(int, char**)
{                       // scope 1 begin

    int i=9;
    potato a(i);        // __scope 1 potato ctor__
    if(i==3)           
      {                    // scope 2 begin
          // replace "potato a(5);" with
          a.modify(5);     // __modify scope 1 potato__
          a.show();        // __of potato in scope 1__
      }                    // scope 2 end

    else

      {                    // scope 3 begin
          // replace "potato a(3);" with
          a.modify(3);     // __modify scope 1 potato__
          a.show();        // __of potato in scope 1__
      }                    // scope 3 end

    a.get_id();
    a.show();         // show contents of scope 1

}                     // scope 1 end

In this version, only 1 potato is ctor'd (and dtor'd). And it is modified one time to a value based on the if clause.

2785528
  • 5,438
  • 2
  • 18
  • 20