-1

Possible Duplicate:
The Definitive C++ Book Guide and List

i have a lot of questions about declaration and implementation, according to most (books, tutorials, blog entries) a class declaration with constructor, methods and member functions:

class Book
{
public:
    Book(const string & author_,
         const string & title_,
         const string & publisher_,
         double price_,
         double weight_);
    string getName()
    {
        string name;
        name = author + ": " + title;
        return name.substr(0, 40);
    }
    double getPrice();
    double getWeight();
private:
    string author, title, publisher;
    double price, weight;
};

i understand all the the access level, the Constructor, reference operator (pointer too!), the pointer operator, but when I read things less trivial like:

class Type
{
public:
    enum TypeT {stringT, intT, doubleT, unknownT};

    // 1. which means "explicit"?
    // 2. what's ": typeId(typeId_)"? after the Ctor declaration
    explicit Type(TypeT typeId_) : typeId(typeId_) {}

    // 3. "const" after the declaration which means?
    BaseValue * newValue() const
    {
        return prototypes[typeId]->clone();
    }

    TypeT getType() const
    {
        return typeId;
    }

    static void init();
    {
        prototypes[stringT] = new Value<string>("");
        prototypes[intT] = new Value<int>(0);
        prototypes[doubleT] = new Value<double>(0);
    }

private:
    TypeT typeId;

    static vector<BaseValue *> prototypes;
};

I feel lost and really have not found clear information about the above points.

Addition to answering my question, if you know somewhere where they have these "tricks" of language

Community
  • 1
  • 1
rkmax
  • 17,633
  • 23
  • 91
  • 176
  • http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean – Maz Nov 22 '11 at 19:40
  • Not really a duplicate, as he is asking three specific questions, although I agree he should refer to the other question for a good place to start reading C++ books. – matthias Nov 22 '11 at 19:49

2 Answers2

4

A good introductory book to C++ should answer your questions.

  1. explicit means the constructor must be mentioned explicitely in the code, the type cannot be constructed automatically from another type when required.
  2. Member initialization. The class member is not constructed with its default constructor, but rather the arguments given here.
  3. The method can be called on a const object.
thiton
  • 35,651
  • 4
  • 70
  • 100
1

1) By default, c++ will assume that any constructor taking one argument of another type can be used as an "implicit conversion" from the arg's type to the constructor's type; in your example, this would allow you to pass a TypeT into any function expecting a Type, and the constructor would assume you want it to run the Type(TypeT) constructor before actually calling the function.

the explicit keyword prevents that from happening; it tells the compiler that you only want that constructor run when you specifically call it.

2) in between the prototype of the constructor and its body, you can (and, for the most part, should) provide an initializer list; When a constructor is run, the system initializes every parent, then every contained member before running the body of the constructor. The initializer list tells the compiler which constructor you want to be run on a given member variable; in the case of your example, you're running the copy constructor on typeId.

If you don't provide an entry in the initializer list for a given member, the system will simply run the default constructor on that member before entering the body of the original class. This means that, should you assign to the member within the body of your class constructor, you'll be writing to that member's memory twice. This is necessary sometimes, but for many cases is simply wasteful.

3) const at the end of a method prototype makes a guarantee to the compiler that that method will not modify any of the member variables within the class instance when it is called. This allows the method to be called on const instances of your class, and, just like placing const on any variables that will remain unchanged, should be done whenever possible to guarrantee correctness and type safety.


As for which books to read, the link in your question's comments would be a good place to start. Since you seem to understand the barebones basics of the language, I would recommend starting with "Effective C++". It presents a list of best practices for C++, and is aimed at someone who understands the C aspects of the language.

matthias
  • 2,419
  • 1
  • 18
  • 27