非初始化类 [英] Non-initialized class

查看:52
本文介绍了非初始化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


今天我遇到了一些我不熟悉的东西,我想要一些

澄清。不幸的是,我没有办法获得一个标准,但是即使我这样做也不会有b $ b,我甚至不确定我是否能够理解它

来回答我的问题问题...


这里是交易:没有实例化类,有问题的代码

调用静态和非静态成员函数,如这个:


class foo {

public:

static void bar(){std :: cout<< "栏" <<的std :: ENDL; }

void bar2(){std :: cout<< " BAR2" <<的std :: ENDL; }

};


无效

func(foo * f)

{

f-> bar();

f-> bar2();

}


int

main()

{

func(NULL);

返回0;

}


这是允许的吗?如果是的话,是否存在这样一种情况:

是一种可行的做事方式?

解决方案

On 2008-10-28 15:53:29 -0400,ejstans< j。******** @ gmail.comsaid:


>

这里是交易:没有实例化类,有问题的代码

调用静态和非静态成员函数,如下所示:


你不必有任何类的实例来调用静态成员

函数。通过空指针调用非静态成员函数

会产生未定义的行为。


>

这是允许的吗?如果是的话,是否存在这样的情况呢?b
是一种可行的做事方式吗?



No.


-

Pete

Roundhouse咨询有限公司( www.versatilecoding.com
的作者
标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


2008-10-28 16:05:49 -0400,Pete Becker< pe **@versatilecoding.comsid:


On 2008-10-28 15:53:29 -0400,ejstans< j。******** @ gmail.comsaid:


>>
这里是交易:没有实例化类,有问题的代码
调用像这样的静态和非静态成员函数:



你不需要任何类的实例来调用静态成员

函数。通过空指针调用非静态成员函数

会产生未定义的行为。


>>
这是允许的吗?如果是的话,是否存在一种可行的做事方式呢?



No.



即否在没有对象的情况下调用非静态成员函数。

静态成员函数没问题。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:教程和参考

www.petebecker.com/tr1book


2008-10-28 20:53,ejstans写道:


您好,


我今天遇到了一些我不熟悉的东西,我想要一些

澄清。不幸的是,我没有办法获得一个标准,但是即使我这样做也不会有b $ b,我甚至不确定我是否能够理解它

来回答我的问题问题...


这里是交易:没有实例化类,有问题的代码

调用静态和非静态成员函数,如这个:


class foo {

public:

static void bar(){std :: cout<< "栏" <<的std :: ENDL; }

void bar2(){std :: cout<< " BAR2" <<的std :: ENDL; }

};


无效

func(foo * f)

{

f-> bar();

f-> bar2();

}


int

main()

{

func(NULL);

返回0;

}


这是允许的吗?如果是的话,是否存在这样的情况呢?b
是一种可行的做事方式吗?



不,这是不允许的。它起作用的原因是因为你没有试图访问任何类成员以及编译器生成的方式来获得
代码。这样做是不可取的。


注意尽管你总是可以使用foo :: bar访问静态成员函数而不使用

一个对象(表示法。


-

Erik Wikstr ?? m


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?

解决方案

On 2008-10-28 15:53:29 -0400, ejstans <j.********@gmail.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)


On 2008-10-28 16:05:49 -0400, Pete Becker <pe**@versatilecoding.comsaid:

On 2008-10-28 15:53:29 -0400, ejstans <j.********@gmail.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)


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


这篇关于非初始化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆