Thread: Class returning a value

  1. #1
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Class returning a value

    Is it possible to have the class (the instance of a class) return a value? I mean like this:
    Code:
    class MyOwnInt
    {
       public:
          int Var;
          char Letter;
    };
    
    int main()
    {
       MyOwnInt MyVar;
       int TempVar = MyVar; <-- Here
       return 0;
    }
    So that when I simply type the name of the instance, a certain data from the class is returned (in this case the Var).
    I believe you can overload the [ ] operators to return values (like an array), but that's not quite what I want. No extra operators. Just the class name.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    almost. just have to do this instead:

    int TempVar = MyVar.Var;

    and you should be all set.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    LOL...when I read the title I couldnt think what you meant!

    The answer is yes btw..but you will need moer overloaded operators to get it to work ok..you need an operator for the types you want to convert to...you can also overload operators like +,-,= etc...

    Code:
    #include<iostream>
    
    class MyOwnInt
    {
    	int Var;
    public:
    	MyOwnInt():Var(0){};//dont forget to initalise
        int operator=(int x){Var += x;return Var;};// for assignment
    	operator int(){return Var;};//for int conversion
    };
    
    int main()
    {
       MyOwnInt MyVar;
    
       MyVar = 10;
    
       int TempVar = MyVar; 
    
       std::cout << TempVar << std::endl;
    
       return 0;
    }
    <edit> oh...and make MyVar private..</edit>

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ah yes, .....the conversion operator(). I keep forgetting about it because I never use it because the dot operator is so much easier to use and almost always works.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Fordy
    LOL...when I read the title I couldnt think what you meant!

    The answer is yes btw..but you will need moer overloaded operators to get it to work ok..you need an operator for the types you want to convert to...you can also overload operators like +,-,= etc...

    <edit> oh...and make MyVar private..</edit>
    Thanks for the quick answer, it works.
    What does the number in Var(0) do? Tried to change it but it didn't seem to change anything.

    Oh, and why do I need to make MyVar private? Do you mean Var? Then it's already done (didn't do it in my example though )
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by elad
    almost. just have to do this instead:

    int TempVar = MyVar.Var;

    and you should be all set.
    Thanks, but I didn't want to access instance variables directly. I only want to print the name of the instance to get the value. I wanted to get rid of the .Name extension (aesthetical reasons ).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Magos
    Thanks for the quick answer, it works.
    What does the number in Var(0) do? Tried to change it but it didn't seem to change anything.

    Oh, and why do I need to make MyVar private? Do you mean Var? Then it's already done (didn't do it in my example though )
    The Var(0) is a way to initialise something....so I set it to zero. Its quicker to use if you have classes and is the only way to initilise const members....To be technical, it constructs a member via its copy constructor (of course an int has no constructor..so its only quicker for member objects)...

    Oh...and you right - I meant Var should be private...not MyVar

    :: Doh!!! ::

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. returning a class
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 06-30-2005, 06:53 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM