由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - static variable in template header
相关主题
关于inline function里的static variablec++ 弱问题:static const char* const 这两个const 分别是什么意思?
c++ inline问题 (转载)基础问题:在header里面define function
C++ 全局变量是怎么回事?C++方法全都内联有什么坏处?
What're the three types of memory allocated for C++ variables?a c++ question for template
inline到底能省多少时间? error LNK2001:的错误如何改正?
C++ 普及课程 (视频):Static Polymorphisminline function是否可以递归?
C++: Static initialization dependency请教: 用stable_sort 在VC++下通过但在g++下通不过
c++中的inline 函数是做什么的?default linkage for const global variable in C/C++
相关话题的讨论汇总
话题: fruit话题: linkage话题: static话题: class话题: orange
进入Programming版参与讨论
1 (共1页)
c***d
发帖数: 996
1
in c++:
Orange.h:
#include "fruit.h"
class Orange : public fruit {
}
Apple.h:
#include "fruit.h"
class Apple : public fruit {
}
fruit.h:
class fruit {
static string brand;
}
string fruit:brand = "dole";
if I compile this I will get multiple definition error at linking phrase. I
can not move the static variable definition to a cpp file since it is a
template file which i can not change. What would be a sensible way to work
around this?
z****e
发帖数: 2024
2
in your Orange.h: Apple.h: use only forward declaration.
class fruit;
class Orange: public fruit{
....
};
do not include is not necessary.
X****r
发帖数: 3557
3
string fruit:brand = "dole";
The above line is a definition for fruit:brand.
In C++, there should be no more than one definition for any
variable, so don't put that line in the header file, which
will be included in different translation units. Instead,
put it in fruit.cc (or fruit.cpp).

【在 c***d 的大作中提到】
: in c++:
: Orange.h:
: #include "fruit.h"
: class Orange : public fruit {
: }
: Apple.h:
: #include "fruit.h"
: class Apple : public fruit {
: }
: fruit.h:

c***d
发帖数: 996
4
If i were coding fruit.h, i would have put it this way. My issue is the
fruit.h has been around for a long time and being used in a lot of places,
so it will be quite some hassle to change that. So the only option for me is
to keep apple.cc and orange.cc in the same file?

【在 X****r 的大作中提到】
: string fruit:brand = "dole";
: The above line is a definition for fruit:brand.
: In C++, there should be no more than one definition for any
: variable, so don't put that line in the header file, which
: will be included in different translation units. Instead,
: put it in fruit.cc (or fruit.cpp).

r****t
发帖数: 10904
5
可能没有 fruit.o 给你 link,他这个是 template header

【在 z****e 的大作中提到】
: in your Orange.h: Apple.h: use only forward declaration.
: class fruit;
: class Orange: public fruit{
: ....
: };
: do not include is not necessary.

r****t
发帖数: 10904
6
namespace {
#include "fruit";
class Apple {
...
};
}
namespace {
#include "fruit";
class Orange {
...
};
}

is

【在 c***d 的大作中提到】
: If i were coding fruit.h, i would have put it this way. My issue is the
: fruit.h has been around for a long time and being used in a lot of places,
: so it will be quite some hassle to change that. So the only option for me is
: to keep apple.cc and orange.cc in the same file?

X****r
发帖数: 3557
7
This is just inviting for future problems. Now you have two
different Fruit class in two unnamed namespaces, plus the
Fruit class in the global scope if also used. They are not
interchangable, e.g. Apple and Orange are no longer derieved
from the same class, totally defeated the purpose of polymorphism.

【在 r****t 的大作中提到】
: namespace {
: #include "fruit";
: class Apple {
: ...
: };
: }
: namespace {
: #include "fruit";
: class Orange {
: ...

t****t
发帖数: 6806
8
my question is, if the fruit.h has been around for a long time and being
used in a lot of places, how were all these files (that included )
linked together?

is

【在 c***d 的大作中提到】
: If i were coding fruit.h, i would have put it this way. My issue is the
: fruit.h has been around for a long time and being used in a lot of places,
: so it will be quite some hassle to change that. So the only option for me is
: to keep apple.cc and orange.cc in the same file?

r****t
发帖数: 10904
9
The fruit class was not designed to be inherited by more than 1 subclass.
The above solution at least makes Apple and Orange behave like
having the same
base class. This is not quite related to polymorphism.
I can see another solution is:
t****t
发帖数: 6806
10
how do you inherit Apple and Orange from Ifruit? don't you need to include
ifruit.h which in turn includes fruit.h?

【在 r****t 的大作中提到】
: The fruit class was not designed to be inherited by more than 1 subclass.
: The above solution at least makes Apple and Orange behave like
: having the same
: base class. This is not quite related to polymorphism.
: I can see another solution is:

相关主题
C++ 普及课程 (视频):Static Polymorphismc++ 弱问题:static const char* const 这两个const 分别是什么意思?
C++: Static initialization dependency基础问题:在header里面define function
c++中的inline 函数是做什么的?C++方法全都内联有什么坏处?
进入Programming版参与讨论
X****r
发帖数: 3557
11
No, as long as Apple and Orange and every other derived class are
included in the same translation unit polymorphism is not an problem.
Your other solution doesn't help either. A link error aoccurs as
long as the definition is included into multiple translation units,
no matter how it is included.

subclass.
same

【在 r****t 的大作中提到】
: The fruit class was not designed to be inherited by more than 1 subclass.
: The above solution at least makes Apple and Orange behave like
: having the same
: base class. This is not quite related to polymorphism.
: I can see another solution is:

r****t
发帖数: 10904
12
you are right. Base class needs to be defined...

【在 t****t 的大作中提到】
: how do you inherit Apple and Orange from Ifruit? don't you need to include
: ifruit.h which in turn includes fruit.h?

z****e
发帖数: 2024
13
是啊,base是一定要define的。这下麻烦了。

【在 r****t 的大作中提到】
: 可能没有 fruit.o 给你 link,他这个是 template header
d****p
发帖数: 685
14
There is nothing wrong as long as you use include guard against fruit.h
Multiple definitions of object with external linkage can be in different
translation units. And the static variable
has internal linkage.
So many cow guys miss this simple issues :-)

【在 c***d 的大作中提到】
: in c++:
: Orange.h:
: #include "fruit.h"
: class Orange : public fruit {
: }
: Apple.h:
: #include "fruit.h"
: class Apple : public fruit {
: }
: fruit.h:

t****t
发帖数: 6806
15
think again...

【在 d****p 的大作中提到】
: There is nothing wrong as long as you use include guard against fruit.h
: Multiple definitions of object with external linkage can be in different
: translation units. And the static variable
: has internal linkage.
: So many cow guys miss this simple issues :-)

z****e
发帖数: 2024
16
include guard只是对于一个translation unit啊。还有,
“Multiple definitions of object with external linkage can be in different
translation units”
我目前只是知道inline能越过multiple definition的这个鸿沟,其他的不都必须一个
definition么?还是我理解有问题?

【在 d****p 的大作中提到】
: There is nothing wrong as long as you use include guard against fruit.h
: Multiple definitions of object with external linkage can be in different
: translation units. And the static variable
: has internal linkage.
: So many cow guys miss this simple issues :-)

d****p
发帖数: 685
17
You really confused me :-(
Header file A:
class Foo
{
..static int a;
};
int Foo::a = 0;
...
We can build N libraries L1, L2, ..., Ln each of which includes file A. And
all these libraries can be linked
together. Wrong?

【在 t****t 的大作中提到】
: think again...
t****t
发帖数: 6806
18
wrong.
PS you mean N object files instead of libraries right?

And

【在 d****p 的大作中提到】
: You really confused me :-(
: Header file A:
: class Foo
: {
: ..static int a;
: };
: int Foo::a = 0;
: ...
: We can build N libraries L1, L2, ..., Ln each of which includes file A. And
: all these libraries can be linked

z****e
发帖数: 2024
19
有啥区别吗,obj打包不就library 了吗?

【在 t****t 的大作中提到】
: wrong.
: PS you mean N object files instead of libraries right?
:
: And

d****p
发帖数: 685
20
How is that?
It doesn't matter whether they are obj files or .a or .so. As long as it has
internal linkage.

【在 t****t 的大作中提到】
: wrong.
: PS you mean N object files instead of libraries right?
:
: And

相关主题
a c++ question for template请教: 用stable_sort 在VC++下通过但在g++下通不过
error LNK2001:的错误如何改正?default linkage for const global variable in C/C++
inline function是否可以递归?也问个template 的问题(C++)
进入Programming版参与讨论
z****e
发帖数: 2024
21
static除了internal linkage以外还有一个概念,就是全局就一个啊。
然后就重复定义拉。
你说说。

has

【在 d****p 的大作中提到】
: How is that?
: It doesn't matter whether they are obj files or .a or .so. As long as it has
: internal linkage.

z****e
发帖数: 2024
22
唉,其实不仅仅是static,任何一个定义,除了inline,都不能重复的。
不信你自己写个member函数,重复定义一下就知道了啊。
哈哈哈哈。
面试官也水了啊。。。
z****e
发帖数: 2024
23
http://tieba.baidu.com/f?kz=822641870
转 一个女大学生揭穿唐骏2004年对女大学生......
123.68.90.*
1楼
mm012345672010-07-09 07:26
唐骏不知你是否还记得04年夏天你到成都和你大学同学见面时当时有几位女大学生一起
吗?我就是其中一位。当时我们去的时候并不知道是去干什么,到了后才明白。。。虽
然你给了非常诱人的条件但是我们是人不是畜生,我们也是有尊严的,我们不靠“卖肉
”生存。自从那次认识了以后只要是有关你的消息我都觉得恶心。以你的身份,你的地
位应该是为人师表,是我们的偶像,但是你的内心却是肮脏不堪,不知道有多少花季少
女死在你手上。你在北邮参加同一首歌的时候我就想揭穿你的丑陋一面,当时考虑你也
是靠着自己的努力才爬到现在这个位置,也许发生在我身上的事只是偶然,但是现在看
来不是这样,所以我站出来了。希望看到这则评论的受伤的朋友都站出来,我们要坚强
捍卫自己的尊严!!!!!
d****p
发帖数: 685
24
Haha, :-) These days excellent interviewees are always way better than
mediocre interviewers like me.
Looked like I made seriously false statements in the above example.
The static member variable in the class Fruit has external linkage. It will
bear internal linkage if it is qualified by const. A non class static
variable however bears internal linkage.
standard 3.5 defines all the rules - enjoy reading the mess!
c***d
发帖数: 996
25
The common way of using fruit.h is to only have one object file referring
to it per shared library. The problem is sometimes the source file(apple.cc)
is rather big and cumbersome. So I want to split into two files. My
original intention is to create two object files, apple.o and orange.o,
compile them into one shared library, say libmyfruit.so. Because of the
double definition error, it seems I have to put Apple and Orange into same
source file.
I tried to compile apple.o into libapple.so, and

【在 c***d 的大作中提到】
: in c++:
: Orange.h:
: #include "fruit.h"
: class Orange : public fruit {
: }
: Apple.h:
: #include "fruit.h"
: class Apple : public fruit {
: }
: fruit.h:

z****e
发帖数: 2024
26
你也太谦虚了。

will

【在 d****p 的大作中提到】
: Haha, :-) These days excellent interviewees are always way better than
: mediocre interviewers like me.
: Looked like I made seriously false statements in the above example.
: The static member variable in the class Fruit has external linkage. It will
: bear internal linkage if it is qualified by const. A non class static
: variable however bears internal linkage.
: standard 3.5 defines all the rules - enjoy reading the mess!

t****t
发帖数: 6806
27
"全局就一个"是external linkage推出来的. internal linkage全局可以不止一个(因
为它根本就不出现在符号表里).
static用在class里不是internal linkage.

【在 z****e 的大作中提到】
: static除了internal linkage以外还有一个概念,就是全局就一个啊。
: 然后就重复定义拉。
: 你说说。
:
: has

t****t
发帖数: 6806
28
大多数情况下, inline就表示是internal linkage了. const也暗示是internal
linkage. 除非显式的写external inline.

【在 z****e 的大作中提到】
: 唉,其实不仅仅是static,任何一个定义,除了inline,都不能重复的。
: 不信你自己写个member函数,重复定义一下就知道了啊。
: 哈哈哈哈。
: 面试官也水了啊。。。

z****e
发帖数: 2024
29
恩,师傅说得对,但是C里边const是默认 extern的,到了C++,才必须写extern,否则
就internal了。
是,inline 和 linkage是完全无关的,和vritual完全无关的,和static完全无关的,
但是我的意思是inline可以重复定义,这是我唯一知道的可以重复定义的东东。

【在 t****t 的大作中提到】
: 大多数情况下, inline就表示是internal linkage了. const也暗示是internal
: linkage. 除非显式的写external inline.

z****e
发帖数: 2024
30
是的是的,从符号表的角度解释是我最喜欢的。

【在 t****t 的大作中提到】
: "全局就一个"是external linkage推出来的. internal linkage全局可以不止一个(因
: 为它根本就不出现在符号表里).
: static用在class里不是internal linkage.

相关主题
C++ Q07: unnamed namespacec++ inline问题 (转载)
c++ does not check const for extern variable?C++ 全局变量是怎么回事?
关于inline function里的static variableWhat're the three types of memory allocated for C++ variables?
进入Programming版参与讨论
t****t
发帖数: 6806
31
oh actually you are right, inline is not related to linkage.
but namespace scope static means internal linkage.

【在 z****e 的大作中提到】
: 恩,师傅说得对,但是C里边const是默认 extern的,到了C++,才必须写extern,否则
: 就internal了。
: 是,inline 和 linkage是完全无关的,和vritual完全无关的,和static完全无关的,
: 但是我的意思是inline可以重复定义,这是我唯一知道的可以重复定义的东东。

z****e
发帖数: 2024
32
namespace scope static means external linkage.
这个真是学到了。但是没有namescope的static都是internal的吧。我是这样认为的。
另外,当virtual要出现多态的时候,inline就失效了。通过object调用,inline成立。

【在 t****t 的大作中提到】
: oh actually you are right, inline is not related to linkage.
: but namespace scope static means internal linkage.

t****t
发帖数: 6806
33
wait, that's a typo. i meant namespace scope static means internal linkage...

立。

【在 z****e 的大作中提到】
: namespace scope static means external linkage.
: 这个真是学到了。但是没有namescope的static都是internal的吧。我是这样认为的。
: 另外,当virtual要出现多态的时候,inline就失效了。通过object调用,inline成立。

z****e
发帖数: 2024
34
挣渡挣渡,惊起一滩鸥鹭!!!

...

【在 t****t 的大作中提到】
: wait, that's a typo. i meant namespace scope static means internal linkage...
:
: 立。

1 (共1页)
进入Programming版参与讨论
相关主题
default linkage for const global variable in C/C++inline到底能省多少时间?
也问个template 的问题(C++)C++ 普及课程 (视频):Static Polymorphism
C++ Q07: unnamed namespaceC++: Static initialization dependency
c++ does not check const for extern variable?c++中的inline 函数是做什么的?
关于inline function里的static variablec++ 弱问题:static const char* const 这两个const 分别是什么意思?
c++ inline问题 (转载)基础问题:在header里面define function
C++ 全局变量是怎么回事?C++方法全都内联有什么坏处?
What're the three types of memory allocated for C++ variables?a c++ question for template
相关话题的讨论汇总
话题: fruit话题: linkage话题: static话题: class话题: orange