some B$ I have with classes

K.I.L.E.R

Retarded moron
Veteran
When I change a value of a "public" variable of a class in my main function that change doesn't carry on to other functions.

Why?

It annoys the living hell out of me. I find no explanation anywhere in my books about it nor online.
 
what ?
I don't understand your problem at all...

Code:
class testclass
{
public:
int m_iVar;

inline int getVar() const { return m_iVar; }
};

void main()
{
testclass anInstance;
anInstance.m_iVar = 5;
cprintf( "%i\n", anInstance.getVar() );
}
That code will output 5 to the console.
 
This is what I want:
MAIN: var1 is 25
25
var1 - 10
15

function1: var1
15
var1 - 10
5

function2: var1
5
var1 - 10
15-5
Press any key to continue

Cuurent output:

MAIN: var1 is 25
25
var1 - 10
15

function1: var1 is 25 again
25
var1 - 10
15

function2: var1 is 25 again
25
var1 - 10
15
Press any key to continue

Code:
class name
{
public:
	int var1;
	void calc(void);

	name();
	~name();
};
name object;

void function1(void);
void function2(void);

int main(void)
{
	name object;

	cout << "MAIN: var1 is 25" << endl;
	cout << object.var1 << endl;

	object.calc();

	cout << "var1 - 10" << endl;
	cout << object.var1 << endl;

	function1();
	function2();

	return 0;
}

void function1(void)
{
	name object;

	cout << endl << "function1: var1 is 25 again" << endl;
	cout << object.var1 << endl;

	object.calc();

	cout << "var1 - 10" << endl;
	cout << object.var1 << endl;
}

void function2(void)
{
	name object;

	cout << endl << "function2: var1 is 25 again" << endl;
	cout << object.var1 << endl;

	object.calc();

	cout << "var1 - 10" << endl;
	cout << object.var1 << endl;
}

void name::calc(void)
{
	var1 -= 10;
}

name::name()
{
	var1 = 25;
}

name::~name()
{
}
 
There a new instance of class 'name' created in both function 1 and function 2. Your constructor sets iVar to 25 in both cases. Your calc function will subtract 10 from them in both cases. In both cases, the correct output is 15.

Move your instantiation in main() of 'name object' outside of main(). Remove the two instantiations in function1() and function2().

And for heavens sake, don't call your classes 'name' and their instantiations 'object'. It makes talking about them an excercise in dancing around what to call them.

edit: p.s. go re-read the chapter on scope. It should be enlightening.
 
Time to re read your book ^^

Just for fun I rewrote your code for it to do what you want. You shouldn't use it if you don't undersand it. But learning through examples is often easier.

So here it is.

Code:
class MyClass
{
private:
   int m_iVar;

public:
   inline MyClass() : m_iVar( -1 ) {}
   inline ~MyClass() {}
   
   /* Accessors */
   inline int getVar() const { return m_iVar; }
   inline void setVar( int iVar ) { m_iVar = iVar; }

   /* Functions */
   inline void decreaseByTen() { m_iVar -= 10; }

};

void dummyfunction1( MyClass* pObject );
void dummyfunction2( MyClass* pObject );

int main(void)
{
   MyClass myObject;

   cout << "myObject.m_iVar = " << myObject.getVar() << endl;

   cout << "var1 - 10" << endl;
   myObject.decreaseByTen();
   cout << myObject.getVar() << endl;

   dummyfunction1( &myObject );
   dummyfunction2( &myObject );

   return 0;
}

void dummyfunction1( MyClass* pObject )
{
   pObject->decreaseByTen();
   cout << pObject->getVar() << endl;
}

void dummyfunction2( MyClass* pObject )
{
   pObject->decreaseByTen();
   cout << pObject->getVar() << endl;
}
 
Thanks. I have done scope a couple of months back and I have just re read about it in my book. I can't believe I never taken scope into consideration when doing it. :oops:
 
Back
Top