473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

specifying ptr to instance of >=2 classes

** Is there a way (C++ syntax) to specify that a pointer should refer to
an object that derives from *two* specific classes?

eg, Suppose I have two classes, 'A' and 'B', and I want to specify that
pointer 'p' refers to an object derived from both. I *don't* want to
invent some class 'Bar' that derives from 'A' and 'B', and specify that
pointer 'p' should derive from that class 'Bar'.

class A
{ ...
}

class B
{ ...
}

class Example
{
public:
A* p; // but I also want p to refer to an object derived from B
}

class Foo : public A, public B
{ ...
}

void main(int argc, char** argv)
{
Example ex;
ex.p = new Foo(); // set Example::p to an object derived from A and B
}

Thanks,
Suzanne

Jul 19 '05 #1
3 1554
> ** Is there a way (C++ syntax) to specify that a pointer should refer to
an object that derives from *two* specific classes?

eg, Suppose I have two classes, 'A' and 'B', and I want to specify that
pointer 'p' refers to an object derived from both. I *don't* want to
invent some class 'Bar' that derives from 'A' and 'B', and specify that
pointer 'p' should derive from that class 'Bar'.

Not easily. Note that Modern C++ Design by Andrei Alexandrescu would
be a good idea to buy. Good luck!
// your two base classes
class A
{};

class B
{};

// a good class
class C : public A, public B
{
};

// a bad class
class D
{};
// test whether T converts to U,
// that is, if T is derived from U
// taken from Modern C++ Design
//
template <class T, class U>
class Conversion
{
typedef char Small;
class Big {char dummy[2];};

static Small Test(U);
static Big Test(...);
static T MakeT();

public:
enum { exists = sizeof(Test(MakeT())) == sizeof(Small) };
};

// catches T == T
//
template <class T>
class Conversion<T, T>
{
public:
enum { exists = false };
};

// issues 'customizable' error
// this one is used for 'false' and has a default ctor
// adapted from MC++D
//
template <bool>
class Check
{
public:
Check() {}
};

// this one is used for 'true' and has a catch-all ctor
//
template <>
class Check<true>
{
public:
Check(...) {}
};
// your pointer class
// Base1 and Base2 are the two required base classes
// T is the pointer type
//
template <class Base1, class Base2, class T>
class MyPointer
{
T* t_;

class ERROR_must_inherit {};
public:
MyPointer(T *t)
{
// must name all the objects since the two statements will be taken
// as function declarations
ERROR_must_inherit the_error;

Check<Conversion<const T, const Base1>::exists > dummy1(the_error);
Check<Conversion<const T, const Base2>::exists > dummy2(the_error);
}
};
int main()
{
MyPointer<A, B, D> p(new D);
MyPointer<A, B, C> p(new C);
}
The first statement will give something like (well.. on Comeau)

line 77: error: no instance of constructor
"Check<<unnamed>>::Check [with <unnamed>=false]" matches the
argument list

The argument types that you used are:
(MyPointer<A, B, D>::ERROR_must_inherit)
Check<Conversion<const T, const Base1>::exists > dummy1(the_error);
^
detected during instantiation of
"MyPointer<Base1, Base2, T>::MyPointer(T *)
[with Base1=A, Base2=B, T=D]" at line 86

line 78: error: no instance of constructor
"Check<<unnamed>>::Check [with <unnamed>=false]" matches the
argument list

The argument types that you used are:
(MyPointer<A, B, D>::ERROR_must_inherit)
Check<Conversion<const T, const Base2>::exists > dummy2(the_error);
^
detected during instantiation of
"MyPointer<Base1, Base2, T>::MyPointer(T *)
[with Base1=A, Base2=B, T=D]" at line 86
Maybe someone has a better solution, or you could adapt this one to
your needs.
Jonathan
Jul 19 '05 #2
Suzanne,

"Suzanne Vogel" <su*************@hotmail.com> wrote in message
news:3f**********@news.unc.edu...
** Is there a way (C++ syntax) to specify that a pointer should refer to
an object that derives from *two* specific classes?

eg, Suppose I have two classes, 'A' and 'B', and I want to specify that
pointer 'p' refers to an object derived from both. I *don't* want to
invent some class 'Bar' that derives from 'A' and 'B', and specify that
pointer 'p' should derive from that class 'Bar'.
Your instincts are correct.

class A
{ ...
}

class B
{ ...
}

class Example
{
public:
A* p; // but I also want p to refer to an object derived from B
}

class Foo : public A, public B
{ ...
}
This class heirarchy is inverted. What you would like is for A and B to
inherit from an abstract base class, which has the interface that you would
like to access polymorphically.

class Base
{
public:
Base(){}
virtual ~Base(){}

virtual void SomeFnc()const=0;
};

class A : public Base
{
public:
A():Base(){}

virtual ~A(){}

virtual void SomeFnc()const{ ... }
};
class B : public Base
{
public:
B():Base(){}

virtual ~B(){}

virtual void SomeFnc()const{ ... }
};

class Example
{
Base* mBasePtr; // avoid public members
public:
Example():mBasePtr(NULL){} // always initialize members

~Example(){ delete mBasePtr; } // avoid memory leaks

void NewA(){ delete mBasePtr; mBasePtr = new A(); }
void NewB(){ delete mBasePtr; mBasePtr = new B(); }

void DoSomeFnc(){ if( mBasePtr ) mBasePtr->SomeFnc(); }
};

void main(int argc, char** argv)
{
Example ex;
ex.p = new Foo(); // set Example::p to an object derived from A and B
Example ex;

ex.DoSomeFnc(); // safely does nothing

ex.NewA(); // new's an A

ex.DoSomeFnc(); // calls A::SomeFnc

ex.NewB(); // new's a B

ex.DoSomeFnc(); // calls B::SomeFnc
}


I would suggest reading Bruce Eckel's "Thinking in C++" and Stroustrup's
"The C++ Programming Language" to get a better understanding of C++'s
facilities and how to use them effectively.
--

Jeff Flinn
Applied Dynamics, International
Jul 19 '05 #3
On Tue, 30 Sep 2003 23:21:49 -0400, Suzanne Vogel
<su*************@hotmail.com> wrote:
** Is there a way (C++ syntax) to specify that a pointer should refer to
an object that derives from *two* specific classes?
Two dynamic casts? Are you talking about the static type of the
pointer, or the dynamic type of the object pointed to?
eg, Suppose I have two classes, 'A' and 'B', and I want to specify that
pointer 'p' refers to an object derived from both. I *don't* want to
invent some class 'Bar' that derives from 'A' and 'B', and specify that
pointer 'p' should derive from that class 'Bar'.

class A
{ ...
}

class B
{ ...
}

class Example
{
public:
A* p; // but I also want p to refer to an object derived from B
Make that private, and add:

template <class T>
void setP(T* t)
{
B* bcheck = t; //error if not derived from b
p = t; //error if not derived from a
}
}

class Foo : public A, public B
{ ...
}

void main(int argc, char** argv)
{
Example ex;
ex.p = new Foo(); // set Example::p to an object derived from A and B


ex.setP(new Foo());
}

Not sure why you would want this though, and not sure if you want a
runtime check. For a runtime check:

void setP(A* ap)
{
dynamic_cast<B&>(*ap); //throws if not derived from B&
p = ap;
}

Tom
Jul 19 '05 #4

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

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
8
by: Christopher Benson-Manica | last post by:
I know I keep asking similar questions, but I really want to do this at least sort of right. Not to mention I got no on-group replies to my previous post :( I desperately want an interface that...
4
by: MikeAth via DotNetMonster.com | last post by:
Hi all, I have two different classes and one instance of the Form. I would like to access the members of the Form instance in both classes without having to create a new instance in the other...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
3
by: Jacek Dziedzic | last post by:
Hello! Suppose I'm writing a library to write a binary representation of some data to a stream. Let's say that to make it extensible in the future I'm preceding the actual data with some info...
5
by: JH | last post by:
Hi I found that a type/class are both a subclass and a instance of base type "object". It conflicts to my understanding that: 1.) a type/class object is created from class statement 2.) a...
4
by: Gre7g Luterman | last post by:
I suppose I was lulled into complacency by how Python makes so many things look like classes, but I'm starting to realize that they're not, are they? I'm writing a C program which handles Python...
1
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.