开发者

Class variable which is object of this class

开发者 https://www.devze.com 2023-01-21 13:18 出处:网络
The title can be confusing, but I\'m wondering is it possible to create program like this one: class family_tree

The title can be confusing, but I'm wondering is it possible to create program like this one:

class family_tree
{
private:
    string name, surname;
    family_tree father(); //fragile point!

public:
    family_tree();
    family_tree(string n, string sur");
    void print();
};

What does the standard say about such declaration? What the good habits of programming saying about it? Is it dangerous?

What is more I can't use the second constructor:

family_tree father("A","B&qu开发者_JAVA百科ot;);

compiler:

expected identifier before string constant

expected ',' or '...' before string constant


class family_tree
{
private:
    string name, surname;
    family_tree father(); //fragile point!

public:
    family_tree();
    family_tree(string n, string sur); // note that I removed a " here.
    void print();
};

It's perfectly valid. Your fragile point is not fragile at all- you have a function that returns a family_tree, and it doesn't matter that it's called on a family_tree object. Whether or not the language provides for you to implicitly cast the const char* string literal to the std::string, I can't recall.


upss..you're right DeadMG I just declared function. Somebody before you(he deleted message?) wrote that I can declare pointer to object family_tree *father; I think it's the best solution for my problem

0

精彩评论

暂无评论...
验证码 换一张
取 消