473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1779

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
1
by: Jing You | last post by:
hi every one, I have got some confused problem when I try to write some custom object by javascript. Look at the example code here: <BODY> <script language="jscript">
5
by: pat270881 | last post by:
hello, i should implement this class: namespace test_1 { class statistician { public: // CONSTRUCTOR
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.