哪种方法定义更好? [英] Which is the better way to define methods?

查看:78
本文介绍了哪种方法定义更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的c ++文字告诉我,我应该用这种方式定义方法:


class Stack

{

int method( double t);

Stack(int s);

...

}


int Stack :: method(double t)

{

/ * behavior * /

}


但我对Java的经验告诉我这种方式更好:


class Stack

{

int method(double t)

{

/ *行为* /

}


Stack(int s)

{

/ *行为* /

}

}


你用哪种方式?我应该使用第一个还是第二个?我宁愿养成良好的习惯,因为我自学C ++而不是必须回到原点,然后再修复坏的。提前感谢您的帮助。

My c++ text tells me that I should define methods this way:

class Stack
{
int method(double t);
Stack(int s);
...
}

int Stack::method(double t)
{
/* behavior */
}

but my experience with Java tells me that this way is better:

class Stack
{
int method(double t)
{
/* behavior */
}

Stack(int s)
{
/* behavior */
}
}

Which way do you use? Should I use the first or the second? I''d
rather develop good habits as I teach myself C++ rather than have to
go back and fix bad ones later. Thank you in advance for the help.

推荐答案

2004年7月7日13:24:25 -0700,Blue Ocean< bl **** *****@hotmail.com>写道:
On 7 Jul 2004 13:24:25 -0700, Blue Ocean <bl*********@hotmail.com> wrote:
我的c ++文本告诉我,我应该用这种方式定义方法:

类Stack
{方法/方法t);
Stack(int s);
...

int Stack :: method(double t)
{
/ *行为* /
}

但我的Java经验告诉我这种方式更好:

类Stack
{
int方法(双t)
{
/ *行为* /
}

Stack(int s)

/ *行为* /
}
}

你用哪种方式?我应该使用第一个还是第二个?我宁愿养成良好的习惯,因为我自学C ++而不是必须稍后回去修复不好的习惯。提前感谢您的帮助。
My c++ text tells me that I should define methods this way:

class Stack
{
int method(double t);
Stack(int s);
...
}

int Stack::method(double t)
{
/* behavior */
}

but my experience with Java tells me that this way is better:

class Stack
{
int method(double t)
{
/* behavior */
}

Stack(int s)
{
/* behavior */
}
}

Which way do you use? Should I use the first or the second? I''d
rather develop good habits as I teach myself C++ rather than have to
go back and fix bad ones later. Thank you in advance for the help.




如果您使用第二种方法,那么您将所有代码放入标题

文件中。有些人不喜欢这样,因为他们认为代码

应该是私有的,或者他们担心来自

大头文件的额外编译时间。 />

但实际上这不是一个大问题。随着越来越多的人编写

模板代码(无论如何你必须将代码放在头文件中)

你的方法变得越来越常见。


john



If you use the second method then you are putting all you code into header
files. Some people don''t like that, either because they think the code
should be private, or they worry about the extra compilation time from
large header files.

But really it''s not a big issue. With more and more people writing
template code (where you have to put the code in the header file anyway)
your method is becoming much more common.

john


自称Blue Ocean的东西写道:
Something that calls itself Blue Ocean wrote:
我的C ++文字告诉我


你正在阅读哪些C ++文本?

我应该用这种方式定义方法:

class Stack {
public :int方法(double t);
Stack(int s);
...
};

inline int Stack :: method(double t){
/ *行为* /
}

但我的Java经验告诉我这种方式更好:

类Stack {
public:int方法(double t){
/ * behavior * /
}

Stack(int s){
/ * behavior * /
}
你用哪种方式?我应该使用第一个还是第二个?
我宁愿养成良好的习惯,因为我自学C ++
而不是必须在以后修复坏的。
My C++ text tells me that
Which C++ text are you reading?
I should define methods this way:

class Stack { public: int method(double t);
Stack(int s);
...
};
inline int Stack::method(double t) {
/* behavior */
}

but my experience with Java tells me that this way is better:

class Stack { public: int method(double t) {
/* behavior */
}

Stack(int s) {
/* behavior */
}
};

Which way do you use? Should I use the first or the second?
I''d rather develop good habits as I teach myself C++
rather than have to go back and fix bad ones later.




你需要*忘掉你的Java习惯。

如果在类定义中定义构造函数,函数或运算符

,它们是内联的函数。

您可以将函数定义移出类定义

,但必须使用''inline''关键字限定它

或者将定义移动到一个单独的*实现*源文件中

,它将被精确编译*一次*!



You need to *unlearn* your Java habits.
If you define constructors, functions or operators
within the class definition, they are inline functions.
You can move the function definition out of the class definition
but you must qualify it with the ''inline'' keyword
or move the definition into a separate *implementation* source file
where it will be compiled exactly *once*!


这是纯粹的化妆品。


我自己更喜欢:


// Poo.hpp(HEADER FILE)


class Poo

{

私人:


双倍数据;


public:


void Increase5Percent();

void Increase20Percent();

};


inline void Poo :: Increase5Percent()

{

double * = 1.05;

}


inline void Poo :: Increase20Percent();

{

double * = 1.2;

}

OVER:


// Poo.hpp(HEADER FILE)


class Poo

{

私人:


双倍数据;


公开:


void Poo :: Increase5Percent()

{

double * = 1.05;

}


void Poo :: Increase20Percent()

{

double * = 1.2;

}

};

考虑一下你是否有23个内联函数,很高兴还有一个很好的

紧凑类声明。

-JKop
It''s purely cosmetic.

I myself would prefer:

// Poo.hpp (HEADER FILE)

class Poo
{
private:

double data;

public:

void Increase5Percent();
void Increase20Percent();
};

inline void Poo::Increase5Percent()
{
double *= 1.05;
}

inline void Poo::Increase20Percent();
{
double *= 1.2;
}
OVER:

// Poo.hpp (HEADER FILE)

class Poo
{
private:

double data;

public:

void Poo::Increase5Percent()
{
double *= 1.05;
}

void Poo::Increase20Percent()
{
double *= 1.2;
}
};
Consider if you had 23 inline functions, it''s nice to still have a nice
compact class declaration.
-JKop


这篇关于哪种方法定义更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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