0
#include <iostream>
#include <string>
using namespace std;

class Box {
    // attributes
public:
    int size = 1;
    string type;
    Box(int size, string type) {
        
    }
};

int main() {
    Box e(2, "b");
    string b = e.type;
    cout << b;
}

I have a little question with this code, these two variables named type in the constructor are the same right? (the type outside of the constructor and the type inside the constructor) but just in different scopes right? like any two variables with the same name and datatype are the same but their values canbe different in different scopes correct?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 4
    "any two variables with the same name and datatype are the same" This is wrong. – MikeCAT Aug 12 '21 at 15:24
  • Oh? Could you explain then? I thought it just depended on what scope they're in but like if I have some int num in two different places in my code you're telling me they can be different? –  Aug 12 '21 at 15:24
  • 3
    What two variables? Be specific. I'm guessing `b` and `e.type`, but your wording is vague to the point that I'm not 100% sure. Your constructor still does nothing, which was addressed in your previous question. – sweenish Aug 12 '21 at 15:25
  • ***like any two variables with the same name and datatype are the same but their values canbe different in different scopes correct?*** They are not the same. They are completely separate independent variables. – drescherjm Aug 12 '21 at 15:27
  • See also: https://stackoverflow.com/questions/68758815/why-doesnt-this-work-classes-and-objects which someone else's question about literally the same code and for which the answers cover what's needed here. – TheUndeadFish Aug 12 '21 at 15:27
  • Well idk for example here right below public I declare a string variable type right? Then in the constructor I have a string variable called type, are you telling me these are not the same at all? –  Aug 12 '21 at 15:28
  • 1
    No they are not the same. In `Box(int size, string type) { }` the `type` variable is a local variable to the constructor. It has no connection to the `type` variable that is a class member. – drescherjm Aug 12 '21 at 15:29
  • But how? They have the exact same datatype and the exact same name right? What makes them different? The only difference I can think of is that they're in different scopes –  Aug 12 '21 at 15:29
  • 1
    ***What makes them different?*** They are declared in different scopes and they are declared as 2 separate variables. Having the same name does not help. – drescherjm Aug 12 '21 at 15:31
  • You appear to be in need of a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Aug 12 '21 at 15:32
  • 3
    Also variables with the same name, same type, and same scope can be different. One example is in recursive call of a function. – MikeCAT Aug 12 '21 at 15:32
  • Well okay if they're different then how would I access the string variable called type that I declared before the constructor inside the constructor after I already declared a parameter in there with the same name and type? –  Aug 12 '21 at 15:33
  • Change `Box(int size, string type) { }` to `Box(int size, string type) size{size}, type{type} { }` – drescherjm Aug 12 '21 at 15:34
  • You can use `this->type` to get the member variable `type` in the constructor body. But inside the constructor the compiler will see `type` on its own as referring to the parameter variable `type`. – mediocrevegetable1 Aug 12 '21 at 15:34
  • 1
    I think the confusion arises because of the ambiguity in the term "the same". Same type, same address, same value, same scope, same storage class, same lifetime... – Tim Randall Aug 12 '21 at 15:39
  • Yeah thanks guys I got what I wanted to understand now I know this was a basic question but I've recently started learning C++ and I wanna make sure I understand every single thing so sorry for asking such a basic question –  Aug 12 '21 at 15:43

3 Answers3

3

No, they are not the same. To clarify their differences, I've prefixed their names with their scope. This code has the same behavior as the OP code.

#include <iostream>
#include <string>
using namespace std;

class Box {
    // attributes
public:
    int box_size = 1;
    string box_type;
    Box(int parm_size, string parm_type) {
        
    }
};

int main() {
    Box main_e(2, "b");
    string main_b = main_e.box_type;
    cout << main_b;
}

It would be nice if the member variables of Box were initialized by the arguments passed into the parameter variables. That is the usual behavior of a constructor.

class Box {
    // attributes
public:
    int box_size = 1;
    string box_type;
    Box(int parm_size, string parm_type)
        : box_size{parm_size}, box_type{parm_type} {
        
    }
};
Eljay
  • 4,648
  • 3
  • 16
  • 27
0

If you want it to be the same you should make it a pointer to e.type:

string* b = &e.type;
cout << *b;
Tevien
  • 101
  • 2
  • ... or reference, which makes them even more same `string& b = e.type;` – Eljay Aug 12 '21 at 15:32
  • 1
    I believe now the confusion and reason for the question is `Box(int size, string type) {` does not initialize the `type` member variable. – drescherjm Aug 12 '21 at 15:32
0

You can reference the member of the class with this-> prefix. this->type. On the other hand it is not recommended to use the same param names as for the members (in case u accidentally forgot to use this->). Instead I met this kind of naming.

struct Foo{
   int param;
   Foo(int _param): param(_param){}
}
JRazek
  • 191
  • 2
  • 11