-
May 31st, 2009, 06:35 AM
#1
casting Class<typename> to Class<const typename>
hello i am having the follow problem.
Code:
template<typename T>
struct A
{
private:
T* t;
};
template<typename T>
struct B
{
inline A<T>& GetA()
{
return a;
}
inline A<const T>& GetA() const // <<<<<<<<
{
return a; // <<<<<<<<<<
}
private:
A<T> a;
};
the problem is obviously that there is no cunstructor in A<const T> to accept A<T>.
i have been brain cracking how to to create the constructor solution.
I was thinking i might as well make a public instead of private.
Any help please?
-
May 31st, 2009, 06:56 AM
#2
Re: casting Class<typename> to Class<const typename>
Is this what you are looking for?
Code:
inline const A1<T>& GetA() const // <<<<<<<<
{
return a; // <<<<<<<<<<
}
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
May 31st, 2009, 09:37 AM
#3
Re: casting Class<typename> to Class<const typename>
no im talking about constructing a A<const T> from A<T>,.
where did u get the "A1" class from.. there isnt any in my code..
-
May 31st, 2009, 10:44 AM
#4
Re: casting Class<typename> to Class<const typename>
I don't think you can.
You can get a const A<T> from an A<T>, but not a A<const T> from a A<T>
That's just the way templates are.
However....
What you could do is provide a way to cast a T to a const T to construct a new A<const T> using a T, but not a A<T>.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|