asking for suggestions.Thanks.

Y

Yan ZHANG

In the following source code,
class CA
{
CB b1;

void funca()
{
b1.funcb();
};
.......
};

void main
{
CB bArray[6];
....
}

I am hoping that, in class CA, b1 is exactly same as bArray[1] in the main
program. That means, for funca in CA, b1.funcb() is same as
bArray[1].funcb().

The only approach i can think about is defining bArray as a global variable.
Any other techniques? Thank you very much.

--
***********MERRY CHRISMAS***************
Thanks and Best Regards

Yan ZHANG
http://www.nict.com.sg/zhang/
 
M

Mole Wang

bArray in your code is a definition, while CA is a declaration of a class.
So, what's your meaning?
I guess you want to get a instance of CA, which has the member b1 identical
with bArray[1]. Simply, you can define a constructor for CA to initiate its
member b1. Whenever you want a object of CA, you just pass bArray[1] to the
constructor.
bArray does not have to be a global variable.
 
Y

Yan ZHANG

Thank you very much for your comments. do you mind give some sample source
code for this? Thanks a lot!


Mole Wang said:
bArray in your code is a definition, while CA is a declaration of a class.
So, what's your meaning?
I guess you want to get a instance of CA, which has the member b1 identical
with bArray[1]. Simply, you can define a constructor for CA to initiate its
member b1. Whenever you want a object of CA, you just pass bArray[1] to the
constructor.
bArray does not have to be a global variable.


In the following source code,
class CA
{
CB b1;

void funca()
{
b1.funcb();
};
......
};

void main
{
CB bArray[6];
...
}

I am hoping that, in class CA, b1 is exactly same as bArray[1] in the main
program. That means, for funca in CA, b1.funcb() is same as
bArray[1].funcb().

The only approach i can think about is defining bArray as a global variable.
Any other techniques? Thank you very much.

--
***********MERRY CHRISMAS***************
Thanks and Best Regards

Yan ZHANG
http://www.nict.com.sg/zhang/
 
M

Mole Wang

class CA {
CB b1;
public:
CA(const CB &b) : b1(b) {}
void funca() {
b1.funcb();
}
};
void main()
{
CB bArray[6];
..................
CA ca(bArray[1]);
}
CB must have a well-defined copy constructor.
I hope this is helpful.

Yan ZHANG said:
Thank you very much for your comments. do you mind give some sample source
code for this? Thanks a lot!


Mole Wang said:
bArray in your code is a definition, while CA is a declaration of a class.
So, what's your meaning?
I guess you want to get a instance of CA, which has the member b1 identical
with bArray[1]. Simply, you can define a constructor for CA to initiate its
member b1. Whenever you want a object of CA, you just pass bArray[1] to the
constructor.
bArray does not have to be a global variable.


In the following source code,
class CA
{
CB b1;

void funca()
{
b1.funcb();
};
......
};

void main
{
CB bArray[6];
...
}

I am hoping that, in class CA, b1 is exactly same as bArray[1] in the main
program. That means, for funca in CA, b1.funcb() is same as
bArray[1].funcb().

The only approach i can think about is defining bArray as a global variable.
Any other techniques? Thank you very much.

--
***********MERRY CHRISMAS***************
Thanks and Best Regards

Yan ZHANG
http://www.nict.com.sg/zhang/
 
J

Jonathan Mcdougall

Yan ZHANG said:
In the following source code,
class CA
{
CB b1;

void funca()
{
b1.funcb();
};
......
};

void main

main() returns an int.

int main()
{
CB bArray[6];
...
}

I am hoping that, in class CA, b1 is exactly same as bArray[1] in the main
program. That means, for funca in CA, b1.funcb() is same as
bArray[1].funcb().

Be careful with the words you use. Differentiate between object and class.
The only approach i can think about is defining bArray as a global
variable.
Any other techniques? Thank you very much.

You need CA to have a reference to an object.

class CA
{
private:
CB &cb_;

public:
CA(CB &cb)
: cb_(cb)
{
}

void f()
{
cb_.g();
}
};

int main()
{
CB cbs[6];

CA ca(cbs[1]);
ca.f(); // calls cbs[1].g()
}


Jonathan
 
J

Jonathan Mcdougall

Mole Wang said:
Yan ZHANG said:
Thank you very much for your comments. do you mind give some sample
source
code for this? Thanks a lot!


Mole Wang said:
bArray in your code is a definition, while CA is a declaration of a
class.
So, what's your meaning?
I guess you want to get a instance of CA, which has the member b1 identical
with bArray[1]. Simply, you can define a constructor for CA to initiate its
member b1. Whenever you want a object of CA, you just pass bArray[1] to the
constructor.
bArray does not have to be a global variable.


In the following source code,
class CA
{
CB b1;

void funca()
{
b1.funcb();
};
......
};

void main
{
CB bArray[6];
...
}

I am hoping that, in class CA, b1 is exactly same as bArray[1] in the main
program. That means, for funca in CA, b1.funcb() is same as
bArray[1].funcb().

The only approach i can think about is defining bArray as a global variable.
Any other techniques? Thank you very much.

Don't top-post. Rearranged.
class CA {
CB b1;

That should be

CB &b1;

if you don't want to copy the object.
public:
CA(const CB &b) : b1(b) {}
void funca() {
b1.funcb();
}
};
void main()

main() ALWAYS returns an int.

int main()
{
CB bArray[6];
.................
CA ca(bArray[1]);
}
CB must have a well-defined copy constructor.
I hope this is helpful.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top