CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    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?

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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.

  3. #3
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    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..

  4. #4
    Join Date
    Nov 2006
    Posts
    1,611

    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
  •  





Click Here to Expand Forum to Full Width

Featured