473,396 Members | 1,768 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,396 software developers and data experts.

template question

3
Hi, I am not very familiar with templates and have a question.

Say I have a class template like this:

Expand|Select|Wrap|Line Numbers
  1. namespace {
  2.    template <class T>
  3.    class A
  4.    {
  5.       public:
  6.          T m_var;
  7.    };
  8. }
  9.  
And in a separate file I have a class like this:
Expand|Select|Wrap|Line Numbers
  1. namespace MyNamespace
  2. {
  3.    class B
  4.    {
  5.    public:
  6.       B(int x=0) : m_x(x) {}
  7.       void SetX(int x) { m_x = x; }
  8.    private:
  9.       int m_x;
  10.    };
  11. }
  12.  
And then in the same file as the class template I have a function to return a pointer to the class:

Expand|Select|Wrap|Line Numbers
  1. A* Create()
  2. {
  3.    A* p = new A<myNamespace::B>();
  4.    return p;
  5. }
  6.  
My question is, when the class A object is created, is there any way to pass an int to the class B object that gets created so that m_x is initialized as 5 or 7, etc.? If not, is there a way to just set x through p?
Sep 21 '07 #1
3 876
weaknessforcats
9,208 Expert Mod 8TB
Yes.

The way your template is defined, the member is a T m_var so that means the default contstructor for T will be called. This will be the default constructor for class B in your example.

So, in the default constructor for class B, you can acquire the starting value by reading a disc file, usibng a singleton, etc. You just can't pass it as an argument.

Other observations:
1) What's with the public data?? This exposes the name of your data variable. Should you ever need to re-design the class, you will need to change every scrap of code written that uses your class. Not a good idea. Make all data private.
2) What's with the m_ ??? Using an m_ does not identify a variable as a class member. You shoulduse this->var. All naming conventions don't work becuse they are not enforceable. Anything not enforceable is a waste of time. I realize this m_ stuff started years ago when everyine was using vi oe somesuch but today we're all using IDE's. In my case, I just place my mouse cursor on top of the variable name and it's declaration appears. I don't have to go look it up.
3) Your Create() function should take an argument that says what type of obkect to build or should be renamed to CreateB().
Sep 22 '07 #2
Linara
3
Thanks for the info.

Is there a way to just call B's SetX function through p? I'm not sure what the syntax would be.
Sep 24 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Is there a way to just call B's SetX function through p? I'm not sure what the syntax would be.
No. p is a pointer to A and there is no SetX in A.

You would need to change your design:
1) make B a member of A and write a SetX for A
or
2) derived B from A and have a virtual SetX in A
or
3) derived B from A and use a Visitor. Read the article in the C/C++ Articles forum.
Sep 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
1
by: Leslaw Bieniasz | last post by:
Hello, I have the following problem: file a.h --------------- template <class T> class A { // some stuff
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.