由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个C++的问题,关于base class的设计的
相关主题
请问c++中操作符可以声明为虚函数吗?请教一个class design的问题
C++: static_cast and dynamic_cast问个 C++到C的问题
C++ class cross reference problemC++ 弱问一个
关于C++中一个Class的大小 (转载)A C++ inheritance question!
【讨论】问一道很简单的C++题。。。。 (转载)C++ virtual function 定义在 derived class 会怎么样?
请问c++为什么会编译失败?friend function 不能virtual 怎么搞呢?
关于 exception 的一个问题c++ covariant type(受不了C++啦!!!)
请叫一个 template class constructor 的问题C++问题
相关话题的讨论汇总
话题: animal话题: cat话题: animaldata话题: class话题: data
进入Programming版参与讨论
1 (共1页)
p*********r
发帖数: 40
1
假设有个base class叫Animal
derived class包括Cat,Dog等等
class Cat: public Animal {
...
};
现在希望在Animal里定义一个成员函数,比如叫processData
其作用是处理一些各种动物共通的数据,比如身高体重之类的
其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
class的member variable
而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其
他一些cat特有的数据,比如胡须长度,这些数据都是CatData这个class的member
variable,当然,CatData是AnimalData的 derived class
现在问题是一般怎么设计这个base class Animal和derived class Cat?
假如把processData的输入参数设为AnimalData类型的reference,那么Cat的
processData就不能用CatData类型的reference,因为override要求必须参数类型一致
但如果把Cat的processData函数的输入也设为AnimalData类型的reference,那么就无法
处理Cat的特有的数据
希望大牛们不吝赐教指点一下,或者扔个链接我自己去网上看,谢谢您!
N******K
发帖数: 10202
2
static_cast?
visitor pattern?

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

N******K
发帖数: 10202
3
http://alumni.cs.ucr.edu/~lgao/teaching/visitor.html
看起来挺恶心的

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

l*******b
发帖数: 2586
4
processData既然是member function 为什么还要有一个animal作为参数呢
oop里函数必须作为second class处理 , 不然就是个灾难

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

g*****y
发帖数: 7271
5
看起来processData不应该设计成成员函数,因为cat不会processData。哈哈
实在想这样设计的话,可以参考visitor pattern。

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

j******o
发帖数: 4219
6
单独做一个static函数吧
b*******s
发帖数: 5216
7
use interface like things rather than inheritance
merely a suggestion

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

Z****7
发帖数: 402
8
c++看起来好难懂

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

N******K
发帖数: 10202
9
智商堪忧的 就不要碰c++

【在 Z****7 的大作中提到】
: c++看起来好难懂
G***l
发帖数: 355
10
visitor pattern解决你的问题很方便。但是有一点点over kill,因为你好像不需要多
种的visitor。
或者你可以用template,这是c++的优势。

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

相关主题
请问c++为什么会编译失败?请教一个class design的问题
关于 exception 的一个问题问个 C++到C的问题
请叫一个 template class constructor 的问题C++ 弱问一个
进入Programming版参与讨论
g*****g
发帖数: 34805
11
因为你的设计有问题,做两个方法,processAnimalData和 processCatData在两个类里
。问题就解决了。后者可以调用前者。

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

s*i
发帖数: 5025
12
generic?

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

d****n
发帖数: 12461
13
这也是灾难吧,都写到h里面去了。所以非infra一概不用。

【在 G***l 的大作中提到】
: visitor pattern解决你的问题很方便。但是有一点点over kill,因为你好像不需要多
: 种的visitor。
: 或者你可以用template,这是c++的优势。

G***l
发帖数: 355
14
按你这么说template都没法用了,用c++不用template还图啥?用tempalte最多编译慢
点而已。

【在 d****n 的大作中提到】
: 这也是灾难吧,都写到h里面去了。所以非infra一概不用。
d****n
发帖数: 12461
15
很难refactor。

【在 G***l 的大作中提到】
: 按你这么说template都没法用了,用c++不用template还图啥?用tempalte最多编译慢
: 点而已。

B********e
发帖数: 1062
16
Try this:
class Animal {
virtual void processData(AnimalData &d)
{
//process common data for an animal
}
};
class Cat : public Animal {
virtual void processData(AnimalData &d)
{
Animal::processData(d);
//process cat specific data
}
};
class Dog : public Animal {
virtual void processData(AnimalData &d)
{
Animal::processData(d);
//process dog specific data
}
};

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

B********e
发帖数: 1062
17
class Animal {
virtual void processData(AnimalData &d) {
{
//process common data for an animal
processPrivateData(d);
}
protected:
virtual void processPrivateData(AnimalData &d) = 0;
};
class Cat : public Animal {
virtual void processPrivateData(AnimalData &d)
{
//process cat specific data
}
};
class Dog : public Animal {
virtual void processPrivateData(AnimalData &d)
{
//process dog specific data
}
};

【在 B********e 的大作中提到】
: Try this:
: class Animal {
: virtual void processData(AnimalData &d)
: {
: //process common data for an animal
: }
: };
: class Cat : public Animal {
: virtual void processData(AnimalData &d)
: {

N******K
发帖数: 10202
18
AnimalData 能覆盖所有情况?

【在 B********e 的大作中提到】
: Try this:
: class Animal {
: virtual void processData(AnimalData &d)
: {
: //process common data for an animal
: }
: };
: class Cat : public Animal {
: virtual void processData(AnimalData &d)
: {

B********e
发帖数: 1062
19
I don't know. Since it's in the design stage, everything is possible.
If it's required to serialize or deserialize the data for all the classes in
a general format, it's not a bad design at all to do this way.
Just one of the options.

【在 N******K 的大作中提到】
: AnimalData 能覆盖所有情况?
g*****g
发帖数: 34805
20
Not a good idea, you'll need casting. It's not a crime to have 2 different
functions. Why are you guys so insisting of using processAnimalData to cover
Cat. An extra processCatData in Cat will do just fine. Cat can process Cat
data and animal data, animal can only process animal data, don't force
animal to process Cat Data. This is OOP 101.

in

【在 B********e 的大作中提到】
: I don't know. Since it's in the design stage, everything is possible.
: If it's required to serialize or deserialize the data for all the classes in
: a general format, it's not a bad design at all to do this way.
: Just one of the options.

相关主题
A C++ inheritance question!c++ covariant type(受不了C++啦!!!)
C++ virtual function 定义在 derived class 会怎么样?C++问题
friend function 不能virtual 怎么搞呢?请教个virtual function的问题
进入Programming版参与讨论
B********e
发帖数: 1062
21
it does not make sense to keep two parallel set of classes
amimal,cat,dog
with
animaldata,catdata,dogdata
my idea is to make the data in a general format. for example in json format.
using the key to find the specific fields to process.

cover
Cat

【在 g*****g 的大作中提到】
: Not a good idea, you'll need casting. It's not a crime to have 2 different
: functions. Why are you guys so insisting of using processAnimalData to cover
: Cat. An extra processCatData in Cat will do just fine. Cat can process Cat
: data and animal data, animal can only process animal data, don't force
: animal to process Cat Data. This is OOP 101.
:
: in

p***o
发帖数: 1252
22
Both ways you lose the ability to detect errors at compile time when
passing DogData to processCatData.

format.

【在 B********e 的大作中提到】
: it does not make sense to keep two parallel set of classes
: amimal,cat,dog
: with
: animaldata,catdata,dogdata
: my idea is to make the data in a general format. for example in json format.
: using the key to find the specific fields to process.
:
: cover
: Cat

m******i
发帖数: 10
23
计算机小白回一下:可以用 pure virtual 在 base class 吗? 在 detived class
overriden. 另外,factory 这个概念可不可以用来设计这个?
a9
发帖数: 21638
24
you will have to call different function when calling this function
if cat{
.processcatdata();
} else {
.processanimaldata();
}

cover
Cat
classes

【在 g*****g 的大作中提到】
: Not a good idea, you'll need casting. It's not a crime to have 2 different
: functions. Why are you guys so insisting of using processAnimalData to cover
: Cat. An extra processCatData in Cat will do just fine. Cat can process Cat
: data and animal data, animal can only process animal data, don't force
: animal to process Cat Data. This is OOP 101.
:
: in

g*****g
发帖数: 34805
25
You are wrong, if you have cat data, route it through processCatData, if you
have Animal data, route it through processAnimalData
Cat can be seen as both an animal and cat, the caller will call the
interface they are interested. There will never be an Animal routed to
processCatData.
it's more like
processCatData(Cat cat) {
processAnimal(cat);
//extra processing for cat.
}
processAnimalData(Animal animal) {
//process animal.
}

【在 a9 的大作中提到】
: you will have to call different function when calling this function
: if cat{
: .processcatdata();
: } else {
: .processanimaldata();
: }
:
: cover
: Cat
: classes

k***e
发帖数: 1931
26
也许理解的不对。CatData从AnimalData继承,那么CatData is a AnimalData,
processData使用(AnimalData*)作为参数,在Cat类中用RTTI判断指针指向的对象是
不是一个CatData不就行了吗?
N******K
发帖数: 10202
27
怎么设计这个基于Json格式的 AnimalData?
这个想法挺好 能仔细展开讲讲么?

format.

【在 B********e 的大作中提到】
: it does not make sense to keep two parallel set of classes
: amimal,cat,dog
: with
: animaldata,catdata,dogdata
: my idea is to make the data in a general format. for example in json format.
: using the key to find the specific fields to process.
:
: cover
: Cat

g*****g
发帖数: 34805
28
因为这不是一个好的做法。两个接口才能保证强类型。

【在 k***e 的大作中提到】
: 也许理解的不对。CatData从AnimalData继承,那么CatData is a AnimalData,
: processData使用(AnimalData*)作为参数,在Cat类中用RTTI判断指针指向的对象是
: 不是一个CatData不就行了吗?

B********e
发帖数: 1062
29
animaldata is just a map of key-val pairs.
say there is a field ‘type’.
in cat.processdata
if type is cat
do cat data.
else
print error
return.

【在 N******K 的大作中提到】
: 怎么设计这个基于Json格式的 AnimalData?
: 这个想法挺好 能仔细展开讲讲么?
:
: format.

N******K
发帖数: 10202
30
要是value的类型不一样呢?
有的是string 有的是自定义的结构体

【在 B********e 的大作中提到】
: animaldata is just a map of key-val pairs.
: say there is a field ‘type’.
: in cat.processdata
: if type is cat
: do cat data.
: else
: print error
: return.

相关主题
问个C++ virtual function的问题 (转载)C++: static_cast and dynamic_cast
弱问一个virtual function的问题C++ class cross reference problem
请问c++中操作符可以声明为虚函数吗?关于C++中一个Class的大小 (转载)
进入Programming版参与讨论
B********e
发帖数: 1062
31
option 1
use boost Any type with std::map
option 2
define the structure in json format. and use json object to replace c++
structure.
if you want to use it with mongo, use bson object.

【在 N******K 的大作中提到】
: 要是value的类型不一样呢?
: 有的是string 有的是自定义的结构体

b*******s
发帖数: 5216
32
it is just an entry-level question
#include
using namespace std;
class Animal
{
public:
virtual void butchered()
{
is_live = false;
cout << "is_live:" << is_live << endl;
}
private:
bool is_live = true;
};
class Dog : public Animal
{
public:
void butchered() override
{
Animal::butchered();
make_hot_pot = true;
cout << "make it hot pot? " << make_hot_pot << endl;
}
private:
bool make_hot_pot = false;
};
int main()
{
Dog dog;
dog.butchered();
}

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

N******K
发帖数: 10202
33
我的意思是 Json object 能包打天下?

【在 B********e 的大作中提到】
: option 1
: use boost Any type with std::map
: option 2
: define the structure in json format. and use json object to replace c++
: structure.
: if you want to use it with mongo, use bson object.

b*******s
发帖数: 5216
34
the only sound solution in this thread

【在 B********e 的大作中提到】
: Try this:
: class Animal {
: virtual void processData(AnimalData &d)
: {
: //process common data for an animal
: }
: };
: class Cat : public Animal {
: virtual void processData(AnimalData &d)
: {

k***e
发帖数: 1931
35
照你这么说C++允许Animal* p = &CatInstance这样的赋值就是大大的破坏了强类型啊。
派生类 is a 基类这是OO基本的哲学原则啊。

【在 g*****g 的大作中提到】
: 因为这不是一个好的做法。两个接口才能保证强类型。
B********e
发帖数: 1062
36
我觉得差不多,我们在实际中用它表示过非常复杂的模型

【在 N******K 的大作中提到】
: 我的意思是 Json object 能包打天下?
G**U
发帖数: 180
37
是我没弄懂lz问题还是。。。我这个实现有什么问题吗?
1 #include
2
3 using namespace std;
4
5 class AnimalData {
6 public:
7 int a;
8 };
9
10 class CatData : public AnimalData {
11 public:
12 int b;
13 };
14
15 class Animal {
16 public:
17 virtual void processData(AnimalData &d)
18 {
19 cout << d.a << endl;
20 }
21 };
22
23 class Cat : public Animal {
24 public:
25 virtual void processData(CatData &d)
26 {
27 Animal::processData(d);
28 cout << d.b << endl;
29 }
30 };
g*****g
发帖数: 34805
38
在Cat里用Animal data是可以的。但在Cat里去判断是什么数据是破坏强类型的。
我不明白你们为啥非要把俩东西挤进一个方法里。如果这个类hierarchy很深,难道就
这么一直instanceOf地一堆if? 两个方法不好吗?

啊。

【在 k***e 的大作中提到】
: 照你这么说C++允许Animal* p = &CatInstance这样的赋值就是大大的破坏了强类型啊。
: 派生类 is a 基类这是OO基本的哲学原则啊。

l*********s
发帖数: 5409
39
Basically, you are taking the burden to decide most derived type from the
java runtime. or reloacting the instanceOf() to the caller. It is not
really more beneficial, but rather personal taste/style.


【在 g*****g 的大作中提到】
: 在Cat里用Animal data是可以的。但在Cat里去判断是什么数据是破坏强类型的。
: 我不明白你们为啥非要把俩东西挤进一个方法里。如果这个类hierarchy很深,难道就
: 这么一直instanceOf地一堆if? 两个方法不好吗?
:
: 啊。

K*******g
发帖数: 26
40
如果Animal, Cat, AnimalData, CatData这样结构定死了确实很难办,但一般很难办的
时候都说明结构本身有问题。在我看来CatData继承AnimalData不是很合适,因为从命
名上说这两个都只是一组数据,没有成员函数的话享受不到继承带来的多态性。
如果可以的话建议改成以下类:
AnimalBasicData:所有动物都有的数据
AnimalXSpecificData:X特有数据
AnimalData {
enum AnimalType{...}
AnimalType type;
AnimalBasicData *basic;
AnimalXSpecificData *xdata;
AnimalYSpecificData *ydata;
...
}
仅供参考:)

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

相关主题
关于C++中一个Class的大小 (转载)关于 exception 的一个问题
【讨论】问一道很简单的C++题。。。。 (转载)请叫一个 template class constructor 的问题
请问c++为什么会编译失败?请教一个class design的问题
进入Programming版参与讨论
N******K
发帖数: 10202
41
举个例子?

【在 B********e 的大作中提到】
: 我觉得差不多,我们在实际中用它表示过非常复杂的模型
B********e
发帖数: 1062
42
网上例子挺多的。
要不, 你说个具体的问题, 大家给你看看用json如何表示?

【在 N******K 的大作中提到】
: 举个例子?
N******K
发帖数: 10202
43
三角形mesh的数据结构:(triangle mesh)
(1)一群点集合 (vetex point)
每个点有很多属性 比如 亮度 方向 等等
(2)一群三角形集合 (cell/face)
每三个点构成一个三角形
每个三角形有很多属性 比如 面积
整个mesh本身有全局属性 可能是好几个矩阵
mesh类要可以扩展 增加属性
这种怎么用json 表达三种mesh
BaseMesh
PolygonMesh
TriangleMesh

【在 B********e 的大作中提到】
: 网上例子挺多的。
: 要不, 你说个具体的问题, 大家给你看看用json如何表示?

B********e
发帖数: 1062
44
VetexPoint {
"x":"..",
"y":"..",
"direction": "",
"brightness":"",
...
}
Triangle {
"p1": { Vetex Point },
"p2" : {Vetex Point },
"p3" : {Vetex Point },
"Area" : "",
...
}
Matrix {
"rows":"",
"columns":"",
"values":[{{Row}}, {{Row}}, ...]
}
TriangleMesh {
"CommonProperty1": {Matrix},
"CommonProperty2": {Matrix},
...
"Points" : [{Vetex Point}, {Vetex Point}, ...]
"Cells" : [{Triangle}, {Triangle}, ...]
}
JSON is just a representation of key-val pair structures.
Other key-val pair structures like PER/BER used in UMTS/LTE
like FIX/FASTFIX used by lots of finacial exchanges.

【在 N******K 的大作中提到】
: 三角形mesh的数据结构:(triangle mesh)
: (1)一群点集合 (vetex point)
: 每个点有很多属性 比如 亮度 方向 等等
: (2)一群三角形集合 (cell/face)
: 每三个点构成一个三角形
: 每个三角形有很多属性 比如 面积
: 整个mesh本身有全局属性 可能是好几个矩阵
: mesh类要可以扩展 增加属性
: 这种怎么用json 表达三种mesh
: BaseMesh

N******K
发帖数: 10202
45
如果要动态删除添加点和三角形 怎么办?
另外你这个matrix 只是数据 如何变为可以定义加减乘除的类?

【在 B********e 的大作中提到】
: VetexPoint {
: "x":"..",
: "y":"..",
: "direction": "",
: "brightness":"",
: ...
: }
: Triangle {
: "p1": { Vetex Point },
: "p2" : {Vetex Point },

B********e
发帖数: 1062
46
As you said, the JSON (like tha matrix) is just the data part.
How about separate the operation away from the data?
starting from
--> deserialized into the operation class
--> do any operations in the class
--> serialized back to .

【在 N******K 的大作中提到】
: 如果要动态删除添加点和三角形 怎么办?
: 另外你这个matrix 只是数据 如何变为可以定义加减乘除的类?

N******K
发帖数: 10202
47
serialize / deserialize 就得复制数据吧?

【在 B********e 的大作中提到】
: As you said, the JSON (like tha matrix) is just the data part.
: How about separate the operation away from the data?
: starting from
: --> deserialized into the operation class
: --> do any operations in the class
: --> serialized back to .

B********e
发帖数: 1062
48
yes. you are right. It means the data copy is needed.

【在 N******K 的大作中提到】
: serialize / deserialize 就得复制数据吧?
N******K
发帖数: 10202
49
是不是这样
matrix 得设计两个类
JsonMatrix 用来存取数据
MathMatrix 用来做运算
两者之间交流通过互相复制数据

【在 B********e 的大作中提到】
: yes. you are right. It means the data copy is needed.
N******K
发帖数: 10202
50
这个每个点的坐标 你用什么精度存? float or double?
可否加个选项 控制 json里面数据的精度?
datatype: double or float

【在 B********e 的大作中提到】
: VetexPoint {
: "x":"..",
: "y":"..",
: "direction": "",
: "brightness":"",
: ...
: }
: Triangle {
: "p1": { Vetex Point },
: "p2" : {Vetex Point },

相关主题
问个 C++到C的问题C++ virtual function 定义在 derived class 会怎么样?
C++ 弱问一个friend function 不能virtual 怎么搞呢?
A C++ inheritance question!c++ covariant type(受不了C++啦!!!)
进入Programming版参与讨论
B********e
发帖数: 1062
51
全用double 行吗?

【在 N******K 的大作中提到】
: 这个每个点的坐标 你用什么精度存? float or double?
: 可否加个选项 控制 json里面数据的精度?
: datatype: double or float

Q**g
发帖数: 183
52
像是经典的Double dispatch pattern.
你大概是想 Dog process DogData, Cat process CatData; 当 Dog 遇上 CatData 或者
Cat 遇上 DogData 就用缺省的 Animal process AnimalData。试试
#include
class Animal;
class Data;
class Cat;
class CatData;
class Dog;
class DogData;
using namespace std;
class Data {
public:
virtual void process(const Animal* animal) const {
cout << "Default Data and Animal" << endl;
}
virtual void process(const Cat* animal) const {
process((const Animal*) animal);
}
virtual void process(const Dog* animal) const {
process((const Animal*) animal);
}
};
class Animal {
public:
virtual void process(const Data* data) const {
data->process(this);
}
};
class Cat : public Animal {
public:
virtual void process(const Data* data) const {
data->process(this);
}
};
class CatData : public Data {
public:
void process(const Cat* animal) const {
cout << "CatData and cat" << endl;
}
};
class Dog : public Animal {
public:
virtual void process(const Data* data) const {
data->process(this);
}
};
class DogData : public Data {
public:
void process(const Dog* animal) const {
cout << "DogData and Dog" << endl;
}
};
int main(int argc, char** argv) {
Dog dog;
Cat cat;
DogData dogData;
CatData catData;
dog.process(&dogData);
dog.process(&catData);
cat.process(&dogData);
cat.process(&catData);
}

【在 p*********r 的大作中提到】
: 假设有个base class叫Animal
: derived class包括Cat,Dog等等
: class Cat: public Animal {
: ...
: };
: 现在希望在Animal里定义一个成员函数,比如叫processData
: 其作用是处理一些各种动物共通的数据,比如身高体重之类的
: 其输入是AnimalData这个class的object,当然身高体重什么的都是AnimalData这个
: class的member variable
: 而在Cat这个class里,希望有一个overriden的processData函数,处理身高体重加上其

N******K
发帖数: 10202
53
你的意思 json里面数据精度必须是定好的 不可以选择 类似c++模板?
我现在有很多c++模板类 我自己搞了一个类似json的格式
josn文件本身存储类的大概信息,具体数据放到另外一个文件
比如
matrix.json 只存储 行 列 的个数 以及数字精度是double还是float还是int
martrix.josn.data 存储具体的数字
这样得用好多文件
是否可以放在一个json里面?

【在 B********e 的大作中提到】
: 全用double 行吗?
1 (共1页)
进入Programming版参与讨论
相关主题
C++问题【讨论】问一道很简单的C++题。。。。 (转载)
请教个virtual function的问题 请问c++为什么会编译失败?
问个C++ virtual function的问题 (转载)关于 exception 的一个问题
弱问一个virtual function的问题请叫一个 template class constructor 的问题
请问c++中操作符可以声明为虚函数吗?请教一个class design的问题
C++: static_cast and dynamic_cast问个 C++到C的问题
C++ class cross reference problemC++ 弱问一个
关于C++中一个Class的大小 (转载)A C++ inheritance question!
相关话题的讨论汇总
话题: animal话题: cat话题: animaldata话题: class话题: data