470,833 Members | 1,352 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Friend class and NULL object member functions

Hi,

I have two simple questions:

1) Do I have to write storage-class-specifier, when I want to declare a
class as friend:

class Bar {};
class Foo { friend ??? Bar; };

VC++8 doesn't complain about it, g++ 3.4.4 throws an error and I can't
find anything about it in c++'98 standart.
2)
#include <iostream>

class Foo {
public: void print() { std::cout << "smth" << std::endl; }
};

int main() {
Foo* f = NULL;
f->print();
}

Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
behavior?

--
KriS

Apr 18 '06 #1
4 1675

KriS wrote:
Hi,

I have two simple questions:

1) Do I have to write storage-class-specifier, when I want to declare a
class as friend:

class Bar {};
class Foo { friend ??? Bar; };

VC++8 doesn't complain about it, g++ 3.4.4 throws an error and I can't
find anything about it in c++'98 standart.
11.4.2 of 2003 states that a "elaborated-type-specifier" is required.

2)
#include <iostream>

class Foo {
public: void print() { std::cout << "smth" << std::endl; }
};

int main() {
Foo* f = NULL;
f->print();
}

Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
behavior?


That is correct.

Apr 18 '06 #2
Noah Roberts wrote:
KriS wrote:
Hi,

I have two simple questions:
snipped 2)
#include <iostream>

class Foo {
public: void print() { std::cout << "smth" << std::endl; }
};

int main() {
Foo* f = NULL;
f->print();
}

Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
behavior?


That is correct.

More specifically, it only "works fine" because Foo::print() does not
reference any member variables. If it did, it would find that the
implicit this Foo* parameter would be NULL and cause a runtime memory
access error. Its probably better to make methods that do not use member
variables static to remove temptation.

Andrew
Apr 20 '06 #3

KriS wrote:
Hi,

I have two simple questions:

1) Do I have to write storage-class-specifier, when I want to declare a
class as friend:

class Bar {};
class Foo { friend ??? Bar; };

VC++8 doesn't complain about it, g++ 3.4.4 throws an error and I can't
find anything about it in c++'98 standart.


you have to tell the compiler either it's a class or a method. In this
case, you clearly wanted to grant class Bar friend access. So use
'friend class Bar;'

Apr 20 '06 #4

andrew wrote:
Noah Roberts wrote:
KriS wrote:
Hi,

I have two simple questions:
snipped 2)
#include <iostream>

class Foo {
public: void print() { std::cout << "smth" << std::endl; }
};

int main() {
Foo* f = NULL;
f->print();
}

Works fine under g++ 3.4.4 and vc++8. As I understand this is undefined
behavior?


That is correct.

More specifically, it only "works fine" because Foo::print() does not
reference any member variables.


"works fine" is one possible outcome of undefined behavior. Because
not referencing a member variable doesn't blow up here and now doesn't
mean it won't later or elsewhere. Undefined is undefined.

Apr 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Bryan Parkoff | last post: by
6 posts views Thread by Adam Parkin | last post: by
5 posts views Thread by Ruben Campos | last post: by
15 posts views Thread by Samee Zahur | last post: by
1 post views Thread by Jing You | last post: by
5 posts views Thread by pat270881 | last post: by
19 posts views Thread by subramanian100in | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.