...

View Full Version : C++ Scopes



satchel
06-16-2009, 10:55 AM
I'm finding that every time I try to do something, I'm sort of stopped by scopes... here's some example 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?

oracleguy
06-16-2009, 08:11 PM
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.



#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.

satchel
06-16-2009, 09:45 PM
Oh, I think I've read about this.. Is this called a "class factory" or something like that?
Thanks a lot :D
I'm not quite sure about a couple of the lines...
On this one, why does it need a reference? :

myclass* makeVars(const int &number)

And what does the "v" in this mean? :

myclass* v = new myclass[number];

Is it just a new class of the pointer?
You've given me a lot to think about :D Thanks!

oracleguy
06-16-2009, 11:00 PM
Oh, I think I've read about this.. Is this called a "class factory" or something like that?

It can be considered that.

Thanks a lot :D
You're welcome.

I'm not quite sure about a couple of the lines...
On this one, why does it need a reference? :

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.


And what does the "v" in this mean? :

myclass* v = new myclass[number];

Is it just a new class of the pointer?
You've given me a lot to think about :D Thanks!

The 'v' is just the name of the variable. It is of the type myclass* which is a pointer to a myclass class.

satchel
06-16-2009, 11:09 PM
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 :D

oracleguy
06-16-2009, 11:16 PM
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 :D

Yep, if it isn't working right for you; post your code and I'll see why it isn't working right.

satchel
06-17-2009, 06:56 PM
Hmm. I'm trying to just create the new class like that, to start with..


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:


Multiple markers at this line
- unused variable ‘v’
- expression in new-declarator must have integral or enumeration type
And this on the second:

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 :)

oracleguy
06-17-2009, 07:16 PM
Hmm. I'm trying to just create the new class like that, to start with..


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.

satchel
06-17-2009, 07:39 PM
That took care of the second error. Still not compiling, same error:

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) :


#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:

#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

oracleguy
06-19-2009, 05:21 AM
That took care of the second error. Still not compiling, same error:

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) :


#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?

satchel
06-19-2009, 05:53 PM
Yep that code runs fine. Thanks for the help. I'll sort the leak out, thanks for mentioning :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum