9 Replies - 371 Views - Last Post: 21 March 2012 - 09:33 PM Rate Topic: -----

#1 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

class variables

Posted 21 March 2012 - 08:33 PM

Hello DIC. I'm just learning c++, and I'm having trouble around the concept of class variables.

int value = 0;
Class::Class(){
    //value = 5;
}Class::getValue(){
    return value;
}


If I try to get the value variable, I'm given an address in memory. However if I uncomment the code in the constructor, I'm given a value of 5. Why am I not given a value of 0 with the comment in place?

Is This A Good Question/Topic? 0
  • +

Replies To: class variables

#2 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: class variables

Posted 21 March 2012 - 08:48 PM

I would need to see the entire code, but it looks like you are initializing value outside of the class. Here is a quick example of how the class would look.

class foo {
public:
    foo(){
        value = 5;
    }
    int returnVal(){
        return value;
    }
private:
   int value;
};




The reason it will give you a crazy number with the line commented out is that the value hasn't been initialized, so the compiler will assign some random garbage number like -121234

edit: the reason your not getting what you want is also because you didn't give the method a return type of int.

This post has been edited by raspinudo: 21 March 2012 - 08:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: class variables

Posted 21 March 2012 - 08:58 PM

Well I'm trying to get the hang of header files. Here's what I have, excluding main.

Header
#ifndef FOO_H
#define FOO_H

class Foo
{
    public:
        Foo();
        ~Foo();
         int getValue(void) ;
    protected:
    private:
    int value;
};

#endif 


cpp file
#include "Foo.h"
#include <iostream>

using namespace std;

int value = 0;

Foo::Foo(){
    //value = 5;
}
int Foo::getValue(){
    return value;
}
Foo::~Foo(){
}



I'm used to Java, you would be able to access the value of 0 but in c++ it's only printing out an address
Was This Post Helpful? 0
  • +
  • -

#4 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: class variables

Posted 21 March 2012 - 09:06 PM

It isn't giving the memory address, it is giving the garbage value I mentioned. The statement int value = 0 isn't assigning the private int value of class Foo. The compiler has no way of knowing that you are referring to the private member of the class.

A side note, memory addresses will be printed in hex. Here is a main method I ran real quick to show you how to print the mem. address

int main(){
Foo f;
int a = (f.getValue());
cout << &a << endl; 
}



note that the ampersand will give the actual mem addr.
on my sys. it printed as 0xbfd24cb8, yours may look different

This post has been edited by raspinudo: 21 March 2012 - 09:07 PM

Was This Post Helpful? 1
  • +
  • -

#5 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: class variables

Posted 21 March 2012 - 09:07 PM

Interesting. Is there any way to let the complier know I want it to assign the value to the private member of the class?
Was This Post Helpful? 0
  • +
  • -

#6 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: class variables

Posted 21 March 2012 - 09:10 PM

A simple way would be to take the java approach I learned of getters and setters. Another way would be an initialization list, which would look like this:

Foo::Foo(int input) : value(input){

}



This will create a constructor that requires initialization of the value variable.

There is another way with pointer arithmetic, but that is probably beyond the scope of this discussion

Also, note the use of '&' in my prev post, I edited it in case you missed it. :bigsmile:

This post has been edited by raspinudo: 21 March 2012 - 09:11 PM

Was This Post Helpful? 1
  • +
  • -

#7 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: class variables

Posted 21 March 2012 - 09:18 PM

Very Informative. I know a little of pointers, but not much. So I assume its safe to say that any variables shouldn't be declared before a constructor has been called.

How would you go about accessing a constant variable then? Is there any way to make immutable variables?
Was This Post Helpful? 0
  • +
  • -

#8 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: class variables

Posted 21 March 2012 - 09:21 PM

Yes there is, the const keyword is what you are looking for my friend.

quick example:
#include <iostream>
using namespace std;

int main(){
    const int foo = 5;
    //foo = 6; /* if you uncomment this, your compiler will hate you */
}




the const makes the variable of type 'read-only' which will tell the compiler to throw a fit if someone tries to reassign it.

One fun thing you can do later on, if you need to remove the const-ness of a variable is called a const cast.

side note: this has been really good for me! always good to sharpen fundamentals.

This post has been edited by raspinudo: 21 March 2012 - 09:26 PM

Was This Post Helpful? 2
  • +
  • -

#9 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: class variables

Posted 21 March 2012 - 09:28 PM

Ahh much appreciated. This is certainly an interesting and complicated language haha
Was This Post Helpful? 0
  • +
  • -

#10 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: class variables

Posted 21 March 2012 - 09:33 PM

No problem at all, were all here to help each other learn. Best of luck in your C++ en devours, if you put in the time to learn it, I'm sure you'll find it very fun and rewarding.

btw, here is what const cast looks like:

#include <iostream>
using namespace std;

int main(){
    int temp = 5;
    const int& foo = temp;
    const_cast<int&>(foo) = 4;
}



In here, I am using a reference, which is an alias for a variable. It acts very much like a pointer, but it doesn't have the ability to be null like a pointer, because references must be initialized at declaration.

Happy Coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1