Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: C++ Scopes

  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts

    C++ Scopes

    I'm finding that every time I try to do something, I'm sort of stopped by scopes... here's some example code:
    Code:
    class myclass {
    int test;
    }var1;
    
    void make_vars(){
    int i=1;
    while(i<10){
       i++;
      myclass var[i];
    }
    }
    
    int main() {
    make_vars();
    var1.test = 1;
    }
    I want to be able to make variables, classes, structs etc.. and be able to get them in the main block, like I can with var1. I know i could declare them in the global scope, but they usually depend on some form of loop or process... What's the best way of doing this?
    Last edited by satchel; 06-16-2009 at 12:19 PM.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It is best to avoid using global variables as much as possible. The code you have has some bugs like, your class definition, the default access specifier on a class is private so you would not be able to read or write to test without setter and getter functions.

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    class myclass {
    public:
    void setTest(const int &n) { test = n };
    int getTest() { return test };
    private:
    int test;
    };
    
    myclass* makeVars(const int &number)
    {
    
    myclass* v = new myclass[number]; for(int x = 0; x < number; x++) {
    v[x].setTest(x);
    } return v;
    } int main() {
    myclass* vars = makeVars(10); for(int x = 0; x < 10; x++) {
    cout << "Variable number " << x << " has the value " << vars[x].getTest() << endl;
    } delete vars; return 0;
    }
    Here is one such way to create a variable number of objects at runtime and pass them around however you like.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    satchel (06-16-2009)

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Oh, I think I've read about this.. Is this called a "class factory" or something like that?
    Thanks a lot
    I'm not quite sure about a couple of the lines...
    On this one, why does it need a reference? :
    Code:
    myclass* makeVars(const int &number)
    And what does the "v" in this mean? :
    Code:
    myclass* v = new myclass[number];
    Is it just a new class of the pointer?
    You've given me a lot to think about Thanks!

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by satchel View Post
    Oh, I think I've read about this.. Is this called a "class factory" or something like that?
    It can be considered that.
    Quote Originally Posted by satchel View Post
    Thanks a lot
    You're welcome.
    Quote Originally Posted by satchel View Post
    I'm not quite sure about a couple of the lines...
    On this one, why does it need a reference? :
    Code:
    myclass* makeVars(const int &number)
    It doesn't need to be a reference but it is faster that way. With an integer it isn't as big of a deal but if it was a class that had a lot of data members that needed to be copied, it could be. Also notice it is has the constant keyword too, which means it passes it by constant reference so that in makeVars, the value of 'number' cannot be changed.

    Quote Originally Posted by satchel View Post
    And what does the "v" in this mean? :
    Code:
    myclass* v = new myclass[number];
    Is it just a new class of the pointer?
    You've given me a lot to think about Thanks!
    The 'v' is just the name of the variable. It is of the type myclass* which is a pointer to a myclass class.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    satchel (06-16-2009)

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Ok one last question :P
    If I wanted to get to the data in the class from.. wherever.. a different function.. I'd just call it with the pointer?
    Thanks for all your help, you've fixed about 6 of my problems all at once

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by satchel View Post
    Ok one last question :P
    If I wanted to get to the data in the class from.. wherever.. a different function.. I'd just call it with the pointer?
    Thanks for all your help, you've fixed about 6 of my problems all at once
    Yep, if it isn't working right for you; post your code and I'll see why it isn't working right.
    OracleGuy

  • #7
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Hmm. I'm trying to just create the new class like that, to start with..
    Code:
    class coords {
    public:
    	int test;
    }var1;
    
    coords* factory(string name) {
    	coords* v = new coords[name];
    	return *v;
    }
    On the "factory" I'm getting this error on the first line:

    Code:
    Multiple markers at this line
    	- unused variable ‘v’
    	- expression in new-declarator must have integral or enumeration type
    And this on the second:
    Code:
    cannot convert ‘coords’ to ‘coords*’ in return
    I'm assuming the second error is caused by the first? I just looked up enumerations... not quite sure why its getting that error? Thanks for the help

  • #8
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by satchel View Post
    Hmm. I'm trying to just create the new class like that, to start with..
    Code:
    class coords {
    public:
    	int test;
    }var1;
    
    coords* factory(string name) {
    	coords* v = new coords[name];
    	return *v;
    }
    Get rid of the var1 at the end of the class declaration. And then on the return statement for the function, remove the *, it should be return v;. You want to return the pointer, by putting the * there you are dereferencing the pointer, which is why it says you are returning a different type than the function specifies.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    satchel (06-17-2009)

  • #9
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    That took care of the second error. Still not compiling, same error:
    Code:
    expression in new-declarator must have integral or enumeration type
    Ahh I've got it. Stupid mistake.. tried to get a string as the key for the array... *slap*
    Ok it's compiled, I'll just check if it works.
    So this works fine (I think) :
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class coords {
    public:
    	int test;
    };
    
    coords* factory(int name) {
    	coords* v = new coords [name];
    	return v;
    }
    
    int main() {
    	int name = 1;
    	coords* test = factory(name);
    	return 0;
    }
    And to get anything back, I'd need another section like factory?

    EDIT:

    I was having a think... Couldn't the same thing be achieved with functions? I haven't tested it fully, but this seems to be working ok:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class myclass {
    public:
    	int test;
    };
    
    int classhandle(int name){
    	myclass test[name];
    	test[name].test = 1;
    	return test[name].test;
    }
    int main(){
    	cout<<classhandle(1);
    	return 1;
    }
    I prefer your version, it's more elegant... but I'm not sure I understand it fully XD
    Last edited by satchel; 06-17-2009 at 07:58 PM.

  • #10
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by satchel View Post
    That took care of the second error. Still not compiling, same error:
    Code:
    expression in new-declarator must have integral or enumeration type
    Ahh I've got it. Stupid mistake.. tried to get a string as the key for the array... *slap*
    Ok it's compiled, I'll just check if it works.
    So this works fine (I think) :
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class coords {
    public:
    	int test;
    };
    
    coords* factory(int name) {
    	coords* v = new coords [name];
    	return v;
    }
    
    int main() {
    	int name = 1;
    	coords* test = factory(name);
    	return 0;
    }
    And to get anything back, I'd need another section like factory?
    The first problem that jumped out at me is that you are leaking memory. Whenever you dynamically allocate memory with 'new', you have to delete it before the program ends.

    I don't see any errors but I didn't run the code through a compiler, is it still giving you trouble?
    OracleGuy

  • #11
    New Coder
    Join Date
    May 2009
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Yep that code runs fine. Thanks for the help. I'll sort the leak out, thanks for mentioning


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •