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 6592425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:33:10+00:00 2026-05-25T17:33:10+00:00

I try to use a abstract class type as a member variable in my

  • 0

I try to use a abstract class type as a member variable in my class definition, but somehow it has some problem. Here is the abstract class type:

class POINT{
   public:
       virtual int point_dim() = 0;
       /*other virtual functions...*/ 
}

And here is the inherited class:

class POINTX : public POINT{
   public:
       int point_dim(){return 10;}
}

I create another class to use POINT as a member variable, since POINT is a pure virtual class, I can only declare it as a pointer:

class SPACE{
   public:
       POINT* m_point;
       /*some other declarations*/
}

But when I use it in my main, it does not work as I expect:

int main(){
    POINTX *ptx = new POINTX();
    SPACE space;
    space.m_point = (POINT*)ptx;
    //some function call use POINT as parameter(pass by reference):
    func(*space.m_point, ....);
}

error happened when func(space.m_pint) is invoked. However, if I do this without class SPACE, it’s ok. e.g.:

int main(){
    POINTX *ptx = new POINTX();
    POINT *m_point = (POINT*)ptx;
    //some function call use POINT as parameter(pass by reference):
    func(*m_point, ....);
}

Anyone knows what’s wrong?

  • 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-05-25T17:33:11+00:00Added an answer on May 25, 2026 at 5:33 pm

    POINT declares:

       virtual int point_dim() const = 0;
    

    Therefore POINTX must have a const method called point_dim:

       int point_dim() const {return 10;}
    

    Without this, it will have two point_dim() methods, one non-const and one const but pure virtual, leaving POINTX as abstract.

    As long as func() takes a reference to a POINT:

    int func(const POINT& pt)
    {
        const int dim = pt.point_dim();
        // ...
    }
    

    You’ll be able to call it like this:

    func(*space.m_point);
    

    Also, note that you don’t need the C-style cast here:

    space.m_point = ptx;
    

    And don’t forget to delete ptx when you’re done!

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

Sidebar

Related Questions

I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I try to use PHPMailer to send registration, activation. etc mail to users: require(class.phpmailer.php);
While mapping class i am getting error 'T' must be a non-abstract type with
I have an abstract generic view-model that I use as a base-class for several
I try to use this code for toggling text on the link: var showText=<span>Open</span>
I try to use the domainpeople.com API and to do I need to use
I try to use the winDBG to debug a dump file. When I run
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I try to use single quotes as much as possible and I've noticed that
I try to use amazon API using PHP. If I use print_r($parsed_xml->Items->Item->ItemAttributes) it show

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.