命名空间和类之间只有默认构造函数的区别 [英] difference between a namespace and class with just default constructor

查看:73
本文介绍了命名空间和类之间只有默认构造函数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种情况有什么区别:


1-持有一个包含一些函数的命名空间:


命名空间MyNamespace < br $>
{

void foo1();

void foo2();

void foo3();

void foo4();

....


}



2-只在一个默认构造函数中保存一个类:


class Myclass

{

public:

Myclass(){};

void foo1();

void foo2();

void foo3();

void foo4();

...


}


谢谢推荐你的答案


优惠!

What is the difference between these two situations:

1- hold a namespace which contains justs some functions:

namespace MyNamespace
{
void foo1();
void foo2();
void foo3();
void foo4();
....

}
and
2- hold a class within just a default constructor:

class Myclass
{
public:
Myclass() {};
void foo1();
void foo2();
void foo3();
void foo4();
...

}

Thanks in advance for your answers

Yous!

推荐答案

Youssef Mesri写道:
Youssef Mesri wrote:
这两种情况有什么区别:

1-持有一个包含一些函数的命名空间:

命名空间MyNamespace
{
void foo1 ();
void foo2 ();
void foo3();
void foo4();
...

}


2-在一个默认构造函数中保持一个类:

class Myclass
{
公共:
Myclass(){};
void foo1() ;
void foo2();
void foo3();
void foo4();
...
~Myclass(){};
}

提前感谢您的回答

Yous!
What is the difference between these two situations:

1- hold a namespace which contains justs some functions:

namespace MyNamespace
{
void foo1();
void foo2();
void foo3();
void foo4();
...

}
and
2- hold a class within just a default constructor:

class Myclass
{
public:
Myclass() {};
void foo1();
void foo2();
void foo3();
void foo4();
...
~Myclass() {};
}

Thanks in advance for your answers

Yous!



Youssef Mesri写道:
Youssef Mesri wrote:
这两种情况有什么区别:

1-持有一个包含一些函数的命名空间:

命名空间MyNamespace
{
void foo1();
void foo2();
void foo3();
void foo4();
...

}


2-只在一个默认构造函数中保存一个类:

class Myclass
{
public:
Myclass(){};
void foo1();
void foo2();
void foo3();
void foo4();
......


预先感谢您的答案
What is the difference between these two situations:

1- hold a namespace which contains justs some functions:

namespace MyNamespace
{
void foo1();
void foo2();
void foo3();
void foo4();
...

}
and
2- hold a class within just a default constructor:

class Myclass
{
public:
Myclass() {};
void foo1();
void foo2();
void foo3();
void foo4();
...

}

Thanks in advance for your answers




一般来说,区别在于访问thos成员函数

一个类,你需要一个该类的实例。在这些成员

函数中,实例可通过''this'指针识别。

基于命名空间的函数没有''this''指针。他们更像是班级的静态成员。你可以写


class Myclass

{

public:

static void foo1();

static void foo2();

...

};


这将是_very_much_就像命名空间中的那些函数一样。


V



Generally speaking, the difference is that to access thos member functions
of a class, you need an instance of that class. And inside those member
functions the instance is identifiable through the ''this'' pointer. The
namespace-based functions do not have the ''this'' pointer. They are more
like static members of the class. You could write

class Myclass
{
public:
static void foo1();
static void foo2();
...
};

which would be _very_much_ like those functions in your namespace.

V


Youssef Mesri写道:
Youssef Mesri wrote:
什么是这两种情况之间的区别:

1-持有一个包含一些功能的命名空间:


[...]

2-只在一个默认构造函数中保存一个类:
What is the difference between these two situations:

1- hold a namespace which contains justs some functions:
[ ... ]
2- hold a class within just a default constructor:




你仍然需要创建一个类的实例来调用它的成员

函数,而没有命名空间的实例。


为了获得更接近命名空间的东西,你的班级将有一个私有的

ctor防止构造对象,而其他函数都将是静态的,所以你可以调用它们而不参考一个特殊的

对象(因为不存在这样的对象)。


-

后来,

杰里。



You still have to create an instance of the class to call its member
functions, whereas there''s no such thing as an instance of a namespace.

To get something closer to a namespace, your class would have a private
ctor to prevent constructing objects, and the other functions would all
be static so you could call them without referring to an particular
object (since no such object will ever exist).

--
Later,
Jerry.


这篇关于命名空间和类之间只有默认构造函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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