Results 1 to 4 of 4

Thread: Bypass implicit operator conversion

  1. #1
    Join Date
    Dec 2008
    Posts
    150

    Bypass implicit operator conversion

    I have a class "A":
    Code:
      class A 
      {
      public: 
         A (B* pB): m_pB (pB) () 
         Operator B* () 
         { 
             m_pB return; 
         }
      private: 
         B* m_pB; 
      }
    Which class "C" derives:

    Code:
      class C: public A 
      { 
      }
    My problem is that I can not implicitly convert C to B:

    Code:
      C* pC = [...]; 
      A* pA = pC; // Works 
      B* pB = pA; // works (using the implicit conversion) 
    
      // But 
      C* pC = [...]; 
      B* pB = pC; // Error: Can not convert (error C2440)
    Is this normal?

    Code:
      C* pC = [...]; 
      B* pB = pC-> GetB ();

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Bypass implicit operator conversion

    Code:
      C* pC = [...]; 
      A* pA = pC; // Works 
      B* pB = pA // Do not work 
      B* pB = *pA; // Should work 
    
      // But 
      C* pC = [...]; 
      B* pB = *pC;  // Should work

  3. #3
    Join Date
    May 2008
    Posts
    271

    Re: Bypass implicit operator conversion

    What you have done is not the best programming. If you really want to keep your conversion of an A to B*, I advise you to do so explicitly like this:

    Code:
    B *pb = static_cast<B*> (*pc) ;

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Bypass implicit operator conversion

    Apart from inheriting from B, no it is impossible to overload operators on simple pointers.

    Otherwise, you can create your pointer class, in which case you can override it for one operator to have a B*.

Similar Threads

  1. Difference between Implicit & Explicit type conversions
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 02:47 PM
  2. Meaning of Explicit and Implicit Conversion
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 30-01-2010, 12:42 PM
  3. Replies: 3
    Last Post: 30-10-2009, 06:27 PM
  4. What are the implicit objects in JSP?
    By Coffee in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 05:27 PM
  5. Difference between Implicit and Explicit Declaration
    By vinodpathak_214 in forum Software Development
    Replies: 3
    Last Post: 16-01-2009, 10:00 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,415,393.64644 seconds with 17 queries