about structures,

hi I am new to C++, can u please explain to me about this syntax.


struct name
{
int &fea_id;
Rwgvector(rfval) &rfval;
name(int &f, Rwgvector(rfval) &r):fea_id(f),rfval(r) {};
};
1
2
3
4
5
6
struct name
{
int &fea_id;
Rwgvector(rfval) &rfval;
name(int &f, Rwgvector(rfval) &r):fea_id(f),rfval(r) {};
};

Line 3 will not compile - you can't use a reference like that.
Line 4 will not compile - you can't use a reference like that, and I don't know what Rwgvector(rfval) is.
Line 5 will not compile - I don't know what Rwgvector(rfval) is, and there is an extra semicolon.
Last edited on
without knowing what Rwgvector is (I'm assuming it's some kind of macro), it's difficult to say.

Although, LB, you're wrong about the references. lines 3 and 5 are just fine.
I tried it once and it wouldn't let me do it, but I suppose it's because when I tried it I didn't know about initializer lists at the time >_< thanks.
hey Disch, in line 5 what is the function of the colon there and can you explain a little about the part after the colon,don't know what it is and what its called.

Thanks.
The colon marks the start of the initializer list. The comma separated list that follows is -- you guessed it! -- the initializer list.

Basically, you can specify arguments for constructors of member and parent classes here, and also initialize intrinsic member variables using the 'constructor like' syntax.

It's better to initialize variables here than to assign them in the body of the constructor if possible, because it's more efficient (at least with custom types?). Using an initializer list means your member variables are constructed straight away with their intended values. Assigning custom type variables means they are default constructed and then initialized.

@Disch I'm not sure if I've quite explained that right - correct me if I'm wrong :)
Last edited on
It marks the start of the initializer list, and it allows you to invoke specific constructors of a class's parents/members.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Parent
{
public:
  Parent(int v); // a constructor that takes an int as a param
};

// say we have a child class, and the child wants to initialize its parent with that above construtor:

class Child : public Parent
{
private:
  string s;
  int i;

public:
  Child()  // the child constructor
    : Parent(5)  // call the parent's constructor
    , s("example")  // call s's constructor
    , i(15)  // call i's ctor
  { }
};
Topic archived. No new replies allowed.