How can I return a value from a pointer?

Probably unclear title, but I'll try to explain better.

Here is what's up:

1
2
3
4
5
6
7
8
9
10
11
struct Square {int number, myClass* myclass};

int main()
{
  vector<myClass> classes;
  myClass unrelated;
  classes.push_back(unrelated);
  Square newClass = {3, &classes.at(0)};
  .
  .
  .


myClass is a class I have. Now, in the class, I have a function what_value and I need to get the classes.at(0) from the pointer to it in another function. But the problem is, how can I do it? I'm completely stumped, here's what I thought of:

newClass.*myclass.what_value();

And it I get an error from the compiler. Basically, how can I do this in another function with a pointer:

classes.at(0).what_value();
closed account (zb0S216C)
The way you're trying to dereference "myclass" is actually the syntax of a pointer to a data-member dereference, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Alpha
{
  int X_;
};
 
typedef int( Alpha:: *Pointer_Type );

int main( )
{
  ::Alpha Bravo_;
  ::Pointer_Type Pointer_( &Alpha::X_ );
  std::cout << ( Bravo_.*Pointer_ ); // Dereference here.
  return( 0 );
}

In order to dereference a pointer, in your case "myclass", alls you have to do is this:

 
*myclass = ...;

See here: http://www.cplusplus.com/doc/tutorial/pointers/

Wazzak
Topic archived. No new replies allowed.