473,462 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to static_cast<MyClass* &>(p) ???

Hello,
I have a class B to mediate/isolate between
implementation and their users (like used in
an abstract factory class pattern):
// B.h
class B {
static B* getImpl();
static void update(B*& previous, B*& current);
};

// I.h
class I: public B {
static I* instance();
static void update(I*& previous, I*& current);
};

// B.cpp
B* B::getImpl()
{
return I::instance();
}

void B::update(B*& previous, B*& current)
{
return I::update((I*&)previous, (I*&)current);
}

I try to use static_cast<I*&>(previous) for (I*&) previous
but failed to compile for the above member function.

Do you think static_cast<I*>(previous) is OK for the cast?

Secondly, do you have a better pattern to achieve the
isolation as well as the task routing?
Thanks and best regards!
Wenjie
Jul 19 '05 #1
4 2213
Wenjie wrote:
Hello,
I have a class B to mediate/isolate between
implementation and their users (like used in
an abstract factory class pattern):
// B.h
class B {
static B* getImpl();
static void update(B*& previous, B*& current);
};

// I.h
class I: public B {
static I* instance();
static void update(I*& previous, I*& current);
};

// B.cpp
B* B::getImpl()
{
return I::instance();
}

void B::update(B*& previous, B*& current)
{
return I::update((I*&)previous, (I*&)current);
}

I try to use static_cast<I*&>(previous) for (I*&) previous
but failed to compile for the above member function.

Do you think static_cast<I*>(previous) is OK for the cast?
Let's re-write this a little.

void B::update(B*& previous, B*& current)
{
I * l_prev = previous; // down cast - bad news
I * l_curr = current; // down cast - bad news

I::update( l_prev, l_curr );

previous = l_prev; // up casting OK
current = l_curr; // up casting OK
}

Basically, your original version is using a B* pointer and saying it's
really an I* pointer. VERY dangerous. You can convert pointers

Secondly, do you have a better pattern to achieve the
isolation as well as the task routing?


What are you really trying to do ?

One suggestion:

class I: public B {

// Create this method instead.

static void update(B*& previous, B*& current);
Jul 19 '05 #2
"Wenjie" <go****@yahoo.com> wrote...
I have a class B to mediate/isolate between
implementation and their users (like used in
an abstract factory class pattern):
// B.h
class B {
static B* getImpl();
static void update(B*& previous, B*& current);
};

// I.h
class I: public B {
static I* instance();
static void update(I*& previous, I*& current);
};

// B.cpp
B* B::getImpl()
{
return I::instance();
}

void B::update(B*& previous, B*& current)
{
return I::update((I*&)previous, (I*&)current);
}

I try to use static_cast<I*&>(previous) for (I*&) previous
but failed to compile for the above member function.
B* and I* are unrelated types. A reference to one cannot be
converted to a reference to the other. Why do you need those
references to pointers anyway? Can't you just have B& or B*?
They would be convertable to I& or I* using static_cast.

Do you think static_cast<I*>(previous) is OK for the cast?
I think your design is utterly flawed. "B" should not know
that there exists "I" who derives from it. Making the base
class aware of any derived classes is against all applicable
principles of OOP.

Having said that, I think you should employ polymorphism here.
What does 'update' function do? Can it be converted to become
a member? Example:

class B {
virtual void update_(B* current) {} // default - nothing
public:
static void update(B* previous, B* current);
};

class I : public B {
void update_(B* current); // real 'I' implementation of
// update functionality -- hidden
};

void B::update(B* previous, B* current) {
previous->update_(current); // virtual call
}

Of course, 'update_' can try converting 'current' into 'I*'
if it needs to, using static_cast or, better yet, dynamic_cast.
And it could call any local static function if necessary.
Secondly, do you have a better pattern to achieve the
isolation as well as the task routing?


I don't. Try the book of patterns.

Victor
Jul 19 '05 #3
"Victor Bazarov" <v.********@attAbi.com> wrote in message

I try to use static_cast<I*&>(previous) for (I*&) previous
but failed to compile for the above member function.
B* and I* are unrelated types. A reference to one cannot be
converted to a reference to the other. Why do you need those
references to pointers anyway? Can't you just have B& or B*?
They would be convertable to I& or I* using static_cast.


Doesn't "I" inherited from "B"? I maintain a linked list of
"I" in "I" but with the 'interface' of "B". The reference is
to let the call update() to update the pointers of the calling
block, which is a hash table in user codes block with those
linked lists. I did try B* but failed (I will examine the
exact difficulty later).

Do you think static_cast<I*>(previous) is OK for the cast?


I think your design is utterly flawed. "B" should not know
that there exists "I" who derives from it. Making the base
class aware of any derived classes is against all applicable
principles of OOP.

Having said that, I think you should employ polymorphism here.
What does 'update' function do? Can it be converted to become
a member? Example:

class B {
virtual void update_(B* current) {} // default - nothing
public:
static void update(B* previous, B* current);
};

class I : public B {
void update_(B* current); // real 'I' implementation of
// update functionality -- hidden
};

void B::update(B* previous, B* current) {
previous->update_(current); // virtual call
}

Of course, 'update_' can try converting 'current' into 'I*'
if it needs to, using static_cast or, better yet, dynamic_cast.
And it could call any local static function if necessary.
Secondly, do you have a better pattern to achieve the
isolation as well as the task routing?


I don't. Try the book of patterns.

Victor

Jul 19 '05 #4
"Wenjie" <go****@yahoo.com> wrote...
"Victor Bazarov" <v.********@attAbi.com> wrote in message

I try to use static_cast<I*&>(previous) for (I*&) previous
but failed to compile for the above member function.


B* and I* are unrelated types. A reference to one cannot be
converted to a reference to the other. Why do you need those
references to pointers anyway? Can't you just have B& or B*?
They would be convertable to I& or I* using static_cast.


Doesn't "I" inherited from "B"? I maintain a linked list of
"I" in "I" but with the 'interface' of "B". The reference is
to let the call update() to update the pointers of the calling
block, which is a hash table in user codes block with those
linked lists. I did try B* but failed (I will examine the
exact difficulty later).


"I" inherits from "B". But "I*" does not inherit from "B*".

With the existing relationship between I and B conversions
work: from I& to B& (implicitly), from I* to B* (implicitly),
from B& to I& (explicitly, use 'static_cast'), from B* to I*
(explicitly, use 'static_cast'). But no other conversions
exist.

struct B {};
struct I : B {};

int foo(B*) { return 1; }
int boo(B&) { return 2; }
int bar(B*&) { return 3; }

int main() {
I i;
foo(&i); // fine. I* to B* conversion
boo(i); // fine. I& to B& conversion
I *pi = &i;
bar(pi); // error. Cannot initialise B*& with I*
}

Victor
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim West | last post by:
Both int a; std::complex<float> b; std::complex<float> c = static_cast<float>(a)*b; and int a; std::complex<float> b;
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
7
by: sarada7 | last post by:
Hi, Is there a way to encode/decode HTML using C++?? Thanks, Sarada.
11
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.