-1

I am trying to implement a class, that has another class as a member.

class Book
{
    class Author;
}

In a .cpp file I am implementing the class Author as follows:

class Author
{
    string firstName;
    string lastName;
public:
    // the constructor for the class
    Author(string _firstName, string _lastName)
    {
        firstName = _firstName;
        lastName = _lastName;
    }
}

I want to do the following: in main, when I declare an object of class Book, I want it to instantiate the constructor of the class Author, from the code above. I was thinking of doing so by passing the parameters to the constructor of the Book class, and then using them to instantiate the constructor of Author, like this:

Book::Book(string a, string b)
{
    Book::Author(a, b);
}

But if I do so, if I have another inner class, as a member of the class Book, that has, let's say - 4 members, I would have too many parameters for the constructor of Book.

So my question is: is there another way to instantiate the constructor of a class that is a member when I create an object of the main class?

And also, how do I print the members of that main class, if these members are classes? I tried to overload the << operator for the inner classes, but I get the error: "invalid use of nonstatic data member."

reevesCFC
  • 13
  • 3
  • @juanchopanza Any data members, you mean. It does have a member class, but not a data member of that (member) class type. – Angew is no longer proud of SO Dec 31 '15 at 08:45
  • 'Instantiate the constructor' is meaningless. There is no inner class here. Unclear what you're asking, or talking about, – user207421 Dec 31 '15 at 08:46
  • @Angew I meant any members. Does a class declaration count as a member? – juanchopanza Dec 31 '15 at 08:46
  • It is not clear what you are trying to achieve. Objects of types Book and Author are completely unrelsted. If you want each book to have an author, you need to add sn Author data member to class Book. (Then some books have no authors, or many authors). – n. m. could be an AI Dec 31 '15 at 08:47
  • @juanchopanza I'll double check the standard, but I'd assume so. After all, `Author` has access to the non-public names in `Book`, just like any other member would. – Angew is no longer proud of SO Dec 31 '15 at 08:47
  • @juanchopanza Yes, it does. And it *is* a member. C++14 N4140 9.2/1: "Members of a class are data members, member functions (9.3), nested types, and enumerators." And 11/1&2 states that members have access to `private` and `protected` names. BTW, this "member classes have access to private" is a change from C++03 to C++11. – Angew is no longer proud of SO Dec 31 '15 at 08:49
  • @reevesCFC Your question shows some rather basic misconceptions about how C++ works. You might want to read through a [good book](http://stackoverflow.com/q/388242/1782465) or tutorial. That should give you a better overall grasp than asking piecewise questions on SO. – Angew is no longer proud of SO Dec 31 '15 at 08:52
  • 1
    Also note that you seem to have an undefined class Book::Author and an unrelated class ::Author defined in the .cpp file. – n. m. could be an AI Dec 31 '15 at 08:56
  • @juanchopanza Which declares a nested type. It's true that the type is *not* defined by the `Author` definition inside the OP's cpp file, though. – Angew is no longer proud of SO Dec 31 '15 at 08:57
  • @juanchopanza 9.7/3: "If class `X` is defined in a namespace scope, a nested class `Y` may be declared in class `X` and later defined in the definition of class `X` or be later defined in a namespace scope enclosing the definition of class `X`." – Angew is no longer proud of SO Dec 31 '15 at 09:02
  • @juanchopanza "or be later defined in a namespace scope enclosing the definition of class `X`." Sure, you'd have to define it as `Book::Author` at that namespace scope, but you don't have to define it lexically inside the containing class. – Angew is no longer proud of SO Dec 31 '15 at 09:16
  • @Angew You're right, I see what you mean now. – juanchopanza Dec 31 '15 at 09:19
  • @n.m. I mean in your example you didn't define `A::B`, you defined a different type `::B` that is not the nested type. That caused some confusion. Here's [an example that shows the definition of the nested type outside of the nesting type's definition](http://ideone.com/Yt5nly). That's the kind of example I was asking for. Sorry for the confusion. – juanchopanza Dec 31 '15 at 09:28
  • Not your real code because there are extremely basic compilation errors (missing semicolons). – Christian Hackl Dec 31 '15 at 09:29

1 Answers1

3

I think you've made a mistake about C++ inner class. There is no inner class only nested class which is not the same.

A Nested class is a class whose definition/declaration is made inside the definition/declaration of another one. But, this relation is a relation on types not on instances of the class.

If you want to have an author for a book you have to build an aggregate or similar, like this:

class Book {
  class Author {
    string name;
    // How to initialize an author (give him a name)
    Author(string n) : name(n) { }
  };
  string title;
  Author theAuthorOfTheBook;
  // How to initialize a book (giving him a title and setting up an author with a given name)

  public :
  Book(string t,string n) : title(t), theAuthorOfTheBook(n) {}
};

The nested definition is interesting because you may have some other concept of authoring for different purpose and not related to book authoring. This nested definition will let you easily talk about book authoring (Book::Author).

I added a field theAuthorOfTheBook to define an attribute for the Book concept. This will probably need to be initialized at Book's construction (see ctors definition).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Thank you. That was quite helpful, got me the basic idea, and I even did the title as a class. If you don't mind me asking, how can I print out the members of the class "Book", if they are all classes? I'm aware of the overloading of the << operator, but it didn't work for me just because the members of the class "Book" are not simple data types, but classes. – reevesCFC Dec 31 '15 at 10:55
  • This is another question, but simply each class has to provide its own `<<` overloading. – Jean-Baptiste Yunès Dec 31 '15 at 11:11