By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

help on templates

P: n/a

I would like to find out how I can effectively make use of templates in
theexample below.

In Class A, I have 3 overloaded member functions "send" for handling
the different messages. Although the messages are different, the way to
handle the message is the same. Hence I'm thinking of using templates
to reduce the number of lines in the code.

------ ClassA.h ------
struct Message1;
struct Message2;
struct Message3;

class A
{
public:
.....
void send(Messages1);
void send(Messages2);
void send(Messages3);
};
------------------------------
I have a calling class that will call the send method in A.
------- CallingClass.cpp ---------
Struct Message1 msg1;
msg1.set(....);
mClassA->send(msg1);
-------------------------------------------

The above works but I wonder if i can use templates to reduce the lines
of codes.

In an attempt to use templates, what I did was:
----- ClassA.h --------------------
class A
{
public:
template <class S> void send(S);
}
--------------------

the calling class remains the same ...

------- CallingClass.cpp ---------
Struct Message1 msg1;
msg1.set(....);
mClassA->send(msg1);
-----------------------------------------------

I was able to compile with the codes above, but I have linker error. It
is unable to locate the implementation of send (Message1) in class A.

I would like to find out how to implement the above using templates.
I'm sorry if I am unclear.

Many thanks in advance!

Regards,
Janice

Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

qn*****@hotmail.com wrote:

In an attempt to use templates, what I did was:
----- ClassA.h --------------------
class A
{
public:
template <class S> void send(S); This means that send is a template function. In the current context, S
could be Message1, Message2 etc.

I would like to find out how to implement the above using templates.


template <class S> void send(S msg)
{
// write your code as usual here, and use S wherever you used
Message1 etc in your original code. (There are some more issues but
they could be pointed out if the code is made available.)

}

Hope this helps.

Nov 22 '05 #2

P: n/a
Neelesh Bodas wrote:
qn*****@hotmail.com wrote:

In an attempt to use templates, what I did was:
----- ClassA.h --------------------
class A
{
public:
template <class S> void send(S);

This means that send is a template function. In the current context, S
could be Message1, Message2 etc.

I would like to find out how to implement the above using templates.


template <class S> void send(S msg)
{
// write your code as usual here, and use S wherever you used
Message1 etc in your original code. (There are some more issues but
they could be pointed out if the code is made available.)

}

Hope this helps.


Depending on your design, you might prefer to use the dynamic
polymorphism of inheritance rather than the static polymorphism of
templates. In particular, you could send messages by a base class with
a standard interface, e.g.,

struct Msg
{
virtual std::string Get() const = 0;
};

struct Msg1 : Msg
{
void Set( /* ... */ ) { /*...*/ }
std::string Get() const { /* ... */ }
};

struct Msg2 : Msg
{
void Set( /* ... */ ) { /*...*/ }
std::string Get() const { /* ... */ }
};

struct A
{
void Send( const Msg& msg )
{
std::cout << msg.Get() << std::endl;
}
};

struct B
{
template<class M>
void Send( const M& msg )
{
std::cout << msg.Get() << std::endl;
}
};
void Foo()
{
Msg1 m1;
Msg2 m2;

m1.Set( /*...*/ );
m2.Set( /*...*/ );

A a;
a.Send( m1 );
a.Send( m2 );

B b;
b.Send( m1 );
b.Send( m2 );
}

Class A implements dynamic polymorphism and class B static polymorphism
(technically, the inheritance specs on Msg1 and Msg2 should be deleted
for static polymorphism, but you get the idea). A tends to be more
efficient in terms of code size, especially if there are many Msg
classes, and B tends to be *slightly* more efficient in terms of
execution speed. I would prefer the first way (class A) if possible.

Cheers! --M

Nov 22 '05 #3

P: n/a

mlimber:
I can't use dynamic polymorphism of inheritance as the messages are
very different in terms of structure and they are legacy codes that I
can't change. I have to make do with Message1, Message2, Message3 and
so on.

Neelesh:
I did just that with send as the member function of Class A. I make use
of S in place of Message 1 etc.

I have a problem in linking the codes. In the calling class, I make a
call like this:

Message1 msg1;
mClassA->send(msg1);

The linker fails to locate the object implementation of
ClassA::send(Message1). It fails to link with the template.

Nov 22 '05 #4

P: n/a
> I have a problem in linking the codes. In the calling class, I make a
call like this:

Message1 msg1;
mClassA->send(msg1);

The linker fails to locate the object implementation of
ClassA::send(Message1). It fails to link with the template.


Unless templates are "export"ed, the definition of templatized function
must be made available at compile time. Since the template code is
instantiated at compile time, hence the liker doesnot come in the
picture here.
It would be better if you could actually show the code which is causing
the error.

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.