By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

static + pointer and reaching the private field of another class

P: n/a
I have a class called foo and my class foo has a private field called
"name".
In my bar.cpp file i have #includes "foo.h" and in bar.cpp I also have
a STATIC function that duplicates an array of pointers to objects type
of foo by duplicating the objects themselves.

So, I had to call the constructor of foo class which must take in the
name to initialize the private name field. To get the name, I
implemented a function called get_name() that returns the name field of
the foo objects.
Everything was fine, get_name () was returning names each of the foo
objects the pointers in the array were pointing to, and I was putting
them in another array as I was taking copies of their values to new
objects.

But I saw an implementation of the same static function that was able
to see the private field "name" using pointers such as :

foo x = new foo ( my_array[i]->name)

so as he incremented my_array he was creating new foo objects and
copying them in another array, and this was working..

I do not understand how this works because bar.cpp and foo.cpp has
nothing in common except bar.cpp includes foo.h . is this is enough for
bar to be able to see the private field of foo objects? -I dont think
so -
Does it have anything to do with static ? or pointers?

ps: when we changed the second implementation as
foo x = new foo (*my_ar[i].name )
it didn't work.
Thanks for reading and looking forward to hearing from you,
sincerely,
mehmet canayaz

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"mehmet canayaz" <me*****@gmail.com> wrote...
I have a class called [...]
We much better understand C++ here. Why don't you simply post your
code instead of explainging what it is?
But I saw an implementation of the same static function that was able
to see the private field "name" using pointers such as :

foo x = new foo ( my_array[i]->name)

so as he incremented my_array he was creating new foo objects and
copying them in another array, and this was working..

I do not understand how this works because bar.cpp and foo.cpp has
nothing in common except bar.cpp includes foo.h . is this is enough for
bar to be able to see the private field of foo objects? -I dont think
so -
Does it have anything to do with static ? or pointers?
Probably. I don't see any static or pointers in your post, though.
Try again, and this time post C++.
ps: when we changed the second implementation as
foo x = new foo (*my_ar[i].name )
it didn't work.


Probably because . has higher precedence than *. Try

foo x = new foo( (*my_ar[i]).name );

V
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.