-1

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");

compiler:

expected identifier before string constant

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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
lvp
  • 2,078
  • 18
  • 24

2 Answers2

2
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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
-1

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

lvp
  • 2,078
  • 18
  • 24