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

Friend class and NULL object member functions

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

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

P: n/a
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

P: n/a

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

P: n/a

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.