Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Struggling with...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 10 Topic 9775 of 9909
Post > Topic >>

Struggling with optional funtionality in a derived class

by "Chris Morley" <chris.morley@[EMAIL PROTECTED] > Jul 8, 2008 at 03:25 PM

Hello,

To start with I don't know if the way I am skinning this cat is a good way
or not, so if there is a better way of doing it all together please let me
know.

I want to enable functionality (members functions + variables) in a
template
class based on a compile time known flag such that if the flag is false
then
trying to use the functions causes a compile error. What I have tried is
deriving from a base class with a flag which works ok to include/exclude
variable and functionality from the optional bit. However, I can't figure
out how then to get access to the base class functions.

I failed miserably at passing the derived type as a template parameter
because I couldn't figure out how to then make the explicit <T, true>
version of the extra stuff class to work.

e.g.
template<bool enable> class CBit { };
template<> class CBit<true> { ... stuff...  };
template<class T, bool incl> class CBoss : public CBit<incl> {...
stuff...};

This is fine but I don't know how to do it with...
template<class T, bool enable> class CBit { };
template<urm?...

(is this actually a partial specialisation because I read on the internet
that you can't keep a type from the original declaration in the partial
specialisation?)

Is there a way of getting the type sent through as a template argument so
I
can use it?

Is there a way of determining the type of your derived class regardless of
anything else? (The types are all static so you can check an assumed type
with satic_cast but can you find it somehow & make a typedef out of it?)

Source of a test program is below.

Thanks,

Chris

-------------------------------------

#include <stdio.h>
#include <conio.h>

template<bool enable>
class CBit { };

template<>
class CBit<true>
{
public:
  CBit() {k=99;}
  void stuff();

private:
  int get() {return k;}
  int k;
};

template<class T, bool incl>
class CBoss
: private CBit<incl>
{
  typedef CBoss<T,incl> myT;
  friend typename CBit<incl>;

public:
  void foo()
  {
   printf("foo()\n");
   stuff();
   printf("\n\n");
  }

private:
  void bar(int g)
  {
   printf("bar() = %d\n", g);
  }
  int bossstuff;

};


int main(int argc, char *argv[ ])
{
  CBoss<int,false> my;
  CBoss<int,true> myTrue;

  printf("sizeof: my=%d, myTrue=%d\n",sizeof(my),sizeof(myTrue));

  // my.foo();
  myTrue.foo();

  _getch();
  return 0;
}



template<> void CBit<true>::stuff()
{
  printf("stuff, true\n");

  typedef CBoss<int,true> D; // How to get this type at compile time???
//Obviously this only works because I know I have a <int,true> in the
sample!

  D* pDerived;
  pDerived = static_cast<D*>(this);

  pDerived->bar( get() );
}






-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 10 Posts in Topic:
Struggling with optional funtionality in a derived class
"Chris Morley"   2008-07-08 15:25:23 
Re: Struggling with optional funtionality in a derived class
Oncaphillis <oncaphill  2008-07-08 20:27:19 
Re: Struggling with optional funtionality in a derived class
"Chris Morley"   2008-07-09 03:28:07 
Re: Struggling with optional funtionality in a derived class
Chris Uzdavinis <cuzda  2008-07-09 13:28:05 
Re: Struggling with optional funtionality in a derived class
Oncaphillis <oncaphill  2008-07-09 14:58:07 
Re: Struggling with optional funtionality in a derived class
Nicola Musatti <nicola  2008-07-09 14:58:08 
Re: Struggling with optional funtionality in a derived class
Yechezkel Mett <ymett.  2008-07-09 14:58:07 
Re: Struggling with optional funtionality in a derived class
"Chris Morley"   2008-07-10 03:28:06 
Re: Struggling with optional funtionality in a derived class
Yechezkel Mett <ymett.  2008-07-10 14:02:36 
Re: Struggling with optional funtionality in a derived class
"Chris Morley"   2008-07-10 19:34:44 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Aug 20 13:04:03 CDT 2008.