English 中文(简体)
C++与Ctor [复制]的班级
原标题:Classes in C++ with Ctor [duplicate]
  • 时间:2011-11-22 19:37:49
  •  标签:
  • c++
This question already has an answer here:
Closed 11 years ago.

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 了解所有准入水平,即联系人、参考操作者(点人!) ,但当我看不到什么像:

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.

回答我的提问,如果你知道有这些“trick”语言的地方的话

最佳回答

对C++的一本好的介绍书应回答你的问题。

  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.
问题回答

(1) 违约时,C++将假定,任何提出另一类论点的建筑商均可作为“单纯的转换”从“增量式”改为“构造”;例如,这将允许你将各种类型转换成任何预期类型的功能,而施工者会假定,在实际指定该功能之前,它会管理类型(TypeT)构造。

www.un.org/Depts/DGACM/index_french.htm 关键词阻止了这种情况的发生;它告诉汇编者,你只想在你具体称呼时施工。

(2) 在建筑商及其身体的原型之间,你可以(而且大部分应当)提供初步设计人名单;如果建筑商经营,系统先向每个家长开放,然后由每个成员组成,然后再管理建筑商。 初步设计者名单显示,制作人希望按特定成员变量进行操作;就你的例子而言,你在<代码>类型Id上重新管理复印件。

如果你不向某个成员提供初始准备名单的条目,则该系统在进入原类别机构之前将对该成员的违约构造进行管理。 这意味着,如果你指派给贵阶层建筑商的会员,你将给该成员写两封信。 这有时是必要的,但在许多情况下只是浪费。

3)const, at the end of a means model make a guarantee to the codificationer that this means will not changing any of the member factors within the category instance when it is calls. 这样就可以在以下几类中标的<代码>const上采用这一方法,而且,就如在任何变数上加上const,这些变数将保持不变,应尽可能加以纠正和类型安全。


读到哪几本书,你提出的问题中的联系就是一个良好的开端。 由于你似乎理解语言的光辉基础,我建议从“有效C++”开始。 它为C++提供了最佳做法清单,针对的是了解语言的C方面的人。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签