0
#include <iostream>

class SomeClass
{
 public: int *SomeNumber;

 SomeClass() { SomeNumber = new int; *SomeNumber = 5; }

 ~SomeClass() { delete SomeNumber; }
 int getSomeNumber(void) { return *SomeNumber; }

};

int main()
{

SomeClass A;
std:: cout << A.getSomeNumber() << std::endl; // outputs 5
std:: cout << A.SomeNumber << std::endl; // outputs SomeNumber address

return 0;
}

How can I get *SomeNumber, not its address, by not using the method getSomeNumber()? If SomeNumber were not a pointer to a int, I could get it with A.SomeNumber

Sorry If I were not clear enough. Thanks in advance.

Daniel
  • 189
  • 1
  • 7
  • You are missing an assignment operator and copy constructor. – pmr Jan 15 '12 at 18:51
  • 1
    @pmr: And a copy constructor. See [The Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – sbi Jan 15 '12 at 18:54
  • @pmr: Darn! Did I really post this within your 5mins edit period? I fell for a stupid beginner's prank... `:)` – sbi Jan 15 '12 at 19:33

3 Answers3

6

Simple:

*A.SomeNumber

It works because . has higher precedence than *, so it's the same as

*(A.SomeNumber)
Thomas
  • 174,939
  • 50
  • 355
  • 478
1

Can't you just do:

std:: cout << (*A.SomeNumber) << std::endl;
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
0

Avoid making your properties public!!!!

You could use visitor design pattern instead of this code like:

class SomeClass
{
 public: int *SomeNumber;

 SomeClass() { SomeNumber = new int; *SomeNumber = 5; }

 void visit( IVisitor* visitor ){ visitor->doSomething(*SomeNumber);}

 ~SomeClass() { delete SomeNumber; }
 int getSomeNumber(void) { return *SomeNumber; }

};

IVisitor is an interface you can implement it and anything you want.

AlexTheo
  • 4,004
  • 1
  • 21
  • 35