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:
|
|
|
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
|
|
|
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.
|
|
|
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... : : 立。
|