睇唔明呢段c++

一個: 是什麼意思?
struct VertexPos
{
        VertexPos():pos(0.0f, 0.0f, 0.0f){}
        VertexPos(float x, float y, float z):pos(x,y,z){}
        VertexPos(const D3DXVECTOR3& v):pos(v){}

        D3DXVECTOR3 pos;
        static IDirect3DVertexDeclaration9* Decl;
};

http://gd.tuwien.ac.at/languages/c/c++oop-pmueller/node10.html

u can check in the section "9.1.2 Construction"

it is used to specify the constructor you want to use
-----------------------------------------------------------------
just read a better example from here:
http://ubuntuforums.org/showthread.php?t=645008
post #5

The part after the colon is called an "initializer list", and is specific to class constructors.


Imagine that class:
        Code:
        class Foo
{
private:
  int bar;
public:
  Foo();
};
When a Foo object is constructed, you will probably want to initialize its private variable(s).

For some types of variables, you can get along with the old fashioned way, using assignments in the constructor:
        Code:
        Foo::Foo()
{
  bar = 0;
  /*...*/
}


Now if bar is not an int but a user-defined type (let's call that type Bar):
        Code:
        Foo::Foo()
{
//  bar = Bar(); // useless, C++ already called bar's default constructor
  bar = Bar(0); // see below
  /*...*/
}
As stated, the first assignment (using the default constructor)is useless as C++ by default constructs all objects using their defaultconstructor.

The second assignment uses much overhead:
1) bar is default constructed
2) a temporary Bar(0) is constructed
3) bar is assigned the temporary object just created in (2)
4) the temporary object is destructed



Also, const and reference members just can't be assigned a value in anyother place than the initializer list (trying to assign to a constmember will give you a compiler error, and references have no way to bedefault-constructed).



The solution to all those problems is such an "initializer list":
        Code:
        class Foo
{
private:
  int m_int;
  const int m_const;
  Bar m_bar;
  Bar& m_ref;
};

Foo::Foo()
  : m_int(0),
    m_const(3), // can't be changed anymore since it's a const
    m_bar(0), // Bar is constructed using the Bar(int) constructor
    m_ref(m_bar) // m_ref references m_bar, this can't be changed anymore
{
  /*...*/
}
This way, even before entering the bracketed code { /*...*/ } youare sure that all members have a correct value assigned to them, in themost efficient way.
In C++, this is considered very bad practise NOT to use initializer lists.

They allow better exception handling too, but let's not dive into that right now...


IOW, always use them, and initialize all your member variables explicitly this way.

[ 本帖最後由 iamsaisai 於 2009-6-10 16:05 編輯 ]

TOP

我知道乜野叫constructor
VertexPos():pos(0.0f, 0.0f, 0.0f){}
我係唔明呢句
:係乜意思?

TOP

0.0f 係唔係小數後一個位

TOP

原帖由 [N.H.K]anime 於 2009-6-10 20:10 發表
0.0f 係唔係小數後一個位


其他答唔到

不過 0.0f 係指 floating point number 值係 0

最後加個f 係因為要聲明該數值係 floating point 而唔係 double precision(default)

TOP

原帖由 tonald 於 2009-6-10 20:01 發表
我知道乜野叫constructor
VertexPos():pos(0.0f, 0.0f, 0.0f){}
我係唔明呢句
:係乜意思?

keyword: member initialization list
一個constructor function
:後面的是member initialization list
pos(0.0f, 0.0f, 0.0f)這一句就是call了D3DXVECTOR3的constructor function

由於constcutor VertexPos() body{ ..... } 入面,你不能call D3DXVECTOR3的costructor
所以要利用D3DXVECTOR3的costructor的話
就要通過member initialization list的syntax

[ 本帖最後由 梁炳 於 2009-6-10 21:13 編輯 ]

TOP

原帖由 tonald 於 2009-6-10 20:01 發表
我知道乜野叫constructor
VertexPos():pos(0.0f, 0.0f, 0.0f){}
我係唔明呢句
:係乜意思?

簡單D咁講, 用 VertexPos() 作為 constructor時, 將 pos 設定為 (0.0f, 0.0f, 0.0f).

其實只係簡短寫法, o個一行等於咁:
  1. VertexPos()
  2. {
  3.         pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  4. }
複製代碼

TOP

原帖由 carychow 於 2009-6-10 21:13 發表

簡單D咁講, 用 VertexPos() 作為 constructor時, 將 pos 設定為 (0.0f, 0.0f, 0.0f).

其實只係簡短寫法, o個一行等於咁:VertexPos()
{
        pos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}

這樣是有分別,你這樣每次使用VertexPos,D3DXVECTOR3的constructor會invoke兩次
只不過結果(result)是一樣

TOP

原帖由 梁炳 於 2009-6-10 21:17 發表

這樣是有分別,你這樣每次使用VertexPos,D3DXVECTOR3的constructor會invoke兩次
只不過結果(result)是一樣

佢志在要知道個冒號有咩意思o者.
只不過搵左Direct3D做 sample, 其他唔好執著啦.

TOP

原帖由 carychow 於 2009-6-10 21:27 發表

佢志在要知道個冒號有咩意思o者.
只不過搵左Direct3D做 sample, 其他唔好執著啦.

我都是純粹在學習language既角度提醒佢
這些細節對學習C++很重要
我都不懂Direct3D
而樓主正學習3D game,我諗佢有興趣知道點樣寫program可以effiecient D

TOP