Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8016557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:34:13+00:00 2026-06-04T20:34:13+00:00

Say I have two classes, One and Two. One and Two are essentially the

  • 0

Say I have two classes, One and Two. One and Two are essentially the same, but Two can convert into One.

#ifndef _ONE_H_
#define _ONE_H_

class One
{
private:
    float m_x;
    float m_y;

public:
    One();
    One(float x, float y);
};

#endif
#ifndef _TWO_H_
#define _TWO_H_

#include "One.h"

class Two
{
private:
    float m_x;
    float m_y;

public:
    Two();
    Two(float x, float y);
    operator One() { return One(m_x, m_y); }
    operator One* () { return &One(m_x, m_y); }
    operator One& () const { return *this; }

    float GetX(void) { return m_x ;}
    float GetY(void) { return m_y ;}

    void Print();
};

#endif

Two has access to One, but One does not have access to Two. In the main.cpp I have:

One a(4.5f, 5.5f);

Two b(10.5, 7.5);

One * c = &b;

I get an error if I try to do the pointer of a One to the address of a Two. The error is “error C2440: ‘initializing’ : cannot convert from ‘Two *’ to ‘One *'”

I cannot for the life of me figure out how to do this, if it is even possible. Any help would be appreciated.

EDIT: Added a new line to Two.h with operator One& () const { return *this; }. The function I’m attempting to use to get this conversion operator to work is void TestRefConstPrint(const One &testClass); With main below, this presents a new error for “cannot convert parameter 1 from Two’ to ‘const One &’.

Inside main I do:

int main()
{
    Two b(10.5, 7.5);

    TestRefConstPrint(b);

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:34:15+00:00Added an answer on June 4, 2026 at 8:34 pm

    Your conversion operators enable the conversions Two -> One and Two -> One*. You are trying to perform a different conversion, Two* -> One*.

    To use your operator One() conversion operator to perform a Two -> One conversion, just do this:

    One c = b;
    

    If you really want to do a Two* -> One* conversion, you can, with an explicit cast:

    One * c = (One *)&b;
    

    or using C++-style casts:

    One * c = reinterpret_cast<One *>(&b);
    

    Note, however, that this does not use either of your conversion operators, and does a very different thing from the Two -> One conversion: it does not create a new object, it makes a pointer-to-One that points to the memory occupied by a Two object. This is generally a bad idea, and it’s unlikely that you want to do this.


    Finally, a comment about your operator One*(): it returns a dangling pointer (a pointer to a local variable which goes out of scope when the function returns). I assume you wrote it in your attempt to get the Two* -> One* conversion to work. You should remove it because it does not do what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two classes. Each class has one parameter. Parameter of first
Let's say I have two models, Classes and People. A Class might have one
Say I have two classes One, the Foo class, interfaces with the user, and
Say I have two classes, and neither of them are GUI components. Class A
Say you have two classes, order and customer: public class Customer{ public int CustomerId
Say I have two separate classes, A and B. I also have Repository class
Let's say we have these two classes: public class Base { public static int
Say I have two classes with the same identifier for a parameterized type trait
Let's say I have two classes. Both have the same member fields (including the
So let's say I have two classes that inherit a base class that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.