473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Non-initialized class

Hello,

I encountered something unfamiliar to me today and I would like some
clarifications. Unfortunately I don't have access to a standard but
even if I did, I'm not even sure if I could understand it well enough
to answer my question...

Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:

class foo {
public:
static void bar() { std::cout << "bar" << std::endl; }
void bar2() { std::cout << "bar2" << std::endl; }
};

void
func(foo* f)
{
f->bar();
f->bar2();
}

int
main()
{
func(NULL);
return 0;
}

Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?
Oct 28 '08 #1
10 1499
On 2008-10-28 15:53:29 -0400, ejstans <j.********@gma il.comsaid:
>
Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:
You don't have to have any instances of a class to call static member
functions. Calling non-static member functions through a null pointer
produces undefined behavior.
>
Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?
No.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 28 '08 #2
On 2008-10-28 16:05:49 -0400, Pete Becker <pe**@versatile coding.comsaid:
On 2008-10-28 15:53:29 -0400, ejstans <j.********@gma il.comsaid:
>>
Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:

You don't have to have any instances of a class to call static member
functions. Calling non-static member functions through a null pointer
produces undefined behavior.
>>
Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?

No.
That is, "No" to calling non-static member functions without an object.
Static member functions are fine.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 28 '08 #3
On 2008-10-28 20:53, ejstans wrote:
Hello,

I encountered something unfamiliar to me today and I would like some
clarifications. Unfortunately I don't have access to a standard but
even if I did, I'm not even sure if I could understand it well enough
to answer my question...

Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:

class foo {
public:
static void bar() { std::cout << "bar" << std::endl; }
void bar2() { std::cout << "bar2" << std::endl; }
};

void
func(foo* f)
{
f->bar();
f->bar2();
}

int
main()
{
func(NULL);
return 0;
}

Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?
No, this is not allowed. The reason it works is because you are not
trying to access any class member and the way the compiler generates to
code. It is never advisable to do something like this.

Note though that you can always access static member functions without
an object using the foo::bar() notation.

--
Erik Wikström
Oct 28 '08 #4
Erik Wikström wrote:
On 2008-10-28 20:53, ejstans wrote:
>Hello,

I encountered something unfamiliar to me today and I would like some
clarifications . Unfortunately I don't have access to a standard but
even if I did, I'm not even sure if I could understand it well enough
to answer my question...

Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:

class foo {
public:
static void bar() { std::cout << "bar" << std::endl; }
void bar2() { std::cout << "bar2" << std::endl; }
};

void
func(foo* f)
{
f->bar();
f->bar2();
}

int
main()
{
func(NULL);
return 0;
}

Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?

No, this is not allowed. The reason it works is [..]
<rant>
The behaviour is undefined. Any attempt to explain why undefined
behaviour actually creates an illusion (yes, an illusion) of a working
program only contributes to the confusion. Now the OP might think,
"it's OK if <blahblah>". But the actual mantra should be "no, it's not
OK, ever". It's similar to "When/Why is it OK to point an unloaded
weapon at your friend's head?" Well, it's NOT. Ever. So, the premise
here ("it works") is wrong. It does NOT work. It's not defined to.
</rant>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 28 '08 #5
On 2008-10-28 21:13, Pete Becker wrote:
On 2008-10-28 16:05:49 -0400, Pete Becker <pe**@versatile coding.comsaid:
>On 2008-10-28 15:53:29 -0400, ejstans <j.********@gma il.comsaid:
>>>
Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:

You don't have to have any instances of a class to call static member
functions. Calling non-static member functions through a null pointer
produces undefined behavior.
>>>
Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?

No.

That is, "No" to calling non-static member functions without an object.
Static member functions are fine.
But you would have to use the Class::Function () notation right? I assume
that that 9.4/2 applies to static member functions as well, more
specifically: "A static member may be referred to using the class member
access syntax, in which case the object-expression is evaluated."

--
Erik Wikström
Oct 28 '08 #6
On 28 Okt, 21:17, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-10-28 20:53, ejstans wrote:
Hello,
I encountered something unfamiliar to me today and I would like some
clarifications. Unfortunately I don't have access to a standard but
even if I did, I'm not even sure if I could understand it well enough
to answer my question...
Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:
class foo {
public:
* * static void bar() { std::cout << "bar" << std::endl; }
* * void bar2() { std::cout << "bar2" << std::endl; }
};
void
func(foo* f)
{
* * f->bar();
* * f->bar2();
}
int
main()
{
* *func(NULL);
* *return 0;
}
Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?

No, this is not allowed. The reason it works is because you are not
trying to access any class member and the way the compiler generates to
code. It is never advisable to do something like this.

Note though that you can always access static member functions without
an object using the foo::bar() notation.
Thanks for the quick replies (that goes to everyone of course).

I've got the gist of the matter, but just to cross all the i's and dot
all the t's, is it in fact valid according to the standard to call a
static member function through a NULL pointer? But invalid (undefined
result?) to do so with a non-static function, even if it doesn't touch
any member data? This is just to satisfy my curiousity, I don't like
this construction and don't plan on using it myself...
Oct 28 '08 #7
On 28 Okt, 22:12, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Erik Wikström wrote:
On 2008-10-28 20:53, ejstans wrote:
Hello,
I encountered something unfamiliar to me today and I would like some
clarifications. Unfortunately I don't have access to a standard but
even if I did, I'm not even sure if I could understand it well enough
to answer my question...
Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:
class foo {
public:
* * static void bar() { std::cout << "bar" << std::endl; }
* * void bar2() { std::cout << "bar2" << std::endl; }
};
void
func(foo* f)
{
* * f->bar();
* * f->bar2();
}
int
main()
{
* *func(NULL);
* *return 0;
}
Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?
No, this is not allowed. The reason it works is [..]

<rant>
The behaviour is undefined. *Any attempt to explain why undefined
behaviour actually creates an illusion (yes, an illusion) of a working
program only contributes to the confusion. *Now the OP might think,
"it's OK if <blahblah>". *But the actual mantra should be "no, it's not
OK, ever". *It's similar to "When/Why is it OK to point an unloaded
weapon at your friend's head?" *Well, it's NOT. *Ever. *So, the premise
here ("it works") is wrong. *It does NOT work. *It's not defined to.
</rant>
Heh! Thanks! I was after this kind of clear-cut answer; that's why I
persisted with the second question as to the actual "legality" of it,
unfortunately posted before I could see your reply.
Oct 28 '08 #8
On 2008-10-28 17:17:36 -0400, Erik Wikström <Er***********@ telia.comsaid:
On 2008-10-28 21:13, Pete Becker wrote:
>>
That is, "No" to calling non-static member functions without an object.
Static member functions are fine.

But you would have to use the Class::Function () notation right? I assume
that that 9.4/2 applies to static member functions as well, more
specifically: "A static member may be referred to using the class member
access syntax, in which case the object-expression is evaluated."
Yes, I think you're right. I wasn't referring to obj->f() (where f is a
static member function), but to C::f() even when no object of type C
exists. That's not what the code in the original question did, so I may
have confused folks.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 28 '08 #9
ejstans wrote:
Hello,

I encountered something unfamiliar to me today and I would like some
clarifications. Unfortunately I don't have access to a standard but
even if I did, I'm not even sure if I could understand it well enough
to answer my question...

Here's the deal: without instantiating the class, the code in question
calls both static and non-static member functions like this:

class foo {
public:
static void bar() { std::cout << "bar" << std::endl; }
void bar2() { std::cout << "bar2" << std::endl; }
};

void
func(foo* f)
{
f->bar();
f->bar2();
}

int
main()
{
func(NULL);
return 0;
}

Is this allowed? And if it is, does a situation exist where it would
be an advisable way of doing things?
In this case, you are trying to dereference a NULL pointer, which will
produce undefined behaviour.
Oct 29 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3759
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
3
12274
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in nonblocking mode in c++? I did something ugly like --- c/c++ mixture --- mkfifo( "testpipe", 777);
2
6126
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
399
12971
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
12
29922
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.