470,630 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,630 developers. It's quick & easy.

Prototype and c++ conversion operator

Can "c++ conversion operator" be considered as an example for
Prototype design pattern?

Following example explains, C++ conversion operator.

#include <iostream>
#include <string>
using namespace std;

class test
{
public:
operator string ()
{
return "test";
}
};

int main()
{
test t;
string s = t;

cout << s << endl;

return 0;
}

--
cross posting to comp.object and comp.lang.c++

Oct 24 '07 #1
1 4106
v4vijayakumar wrote:
Can "c++ conversion operator" be considered as an example for
Prototype design pattern?

Following example explains, C++ conversion operator.

#include <iostream>
#include <string>
using namespace std;

class test
{
public:
operator string ()
{
return "test";
}
};

int main()
{
test t;
string s = t;

cout << s << endl;

return 0;
}
IMHO, no. The point of the Prototype patters is that there exists
an object [of the same type] that has some traits you want your new
object to inherit, so to speak, and some traits you want your new
object to have different. The main point is that the prototype is
of the same type as the object you need to produce.

Generally speaking, the prototype in your case here is not the 't',
but the temporary object its conversion function returns. So, to
some extend you can probably claim that the conversion function does
play the role of prototyping mechanism, but the implementation of
this pattern is not in the conversion function but in the fact that
you copy-construct your 's' string in 'main'.

Just my $0.02.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Master of C++ | last post: by
1 post views Thread by Christophe Poucet | last post: by
5 posts views Thread by Vijai Kalyan | last post: by
16 posts views Thread by frs | last post: by
6 posts views Thread by Arne Schmitz | last post: by
11 posts views Thread by Steve Gough | last post: by
3 posts views Thread by Steve Richter | last post: by
8 posts views Thread by Nikola | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.