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

  • Home
  • SEARCH
  • 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 8914579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:45:29+00:00 2026-06-15T04:45:29+00:00

I have the following program: #include <iostream> class Base {}; class Deriv : public

  • 0

I have the following program:

#include <iostream>

class Base {};

class Deriv : public Base
{
    public:
        int data;
        Deriv(int data): data(data) {} 
};

int main()
{
    Base *t = new Deriv(2);
    std::cout << t->data << std::endl;
}

When I compile it, I’m getting the error:

x.cpp: In function ‘int main()’:
x.cpp:15:21: error: ‘class Base’ has no member named ‘data’

How can I get access to data field (note that I don’t want to use Deriv *t = new Deriv(2))?

  • 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-15T04:45:31+00:00Added an answer on June 15, 2026 at 4:45 am

    You have declared your pointer to be of type Base and the compiler is correct, there is no member data in that class. You would need to cast the pointer back up to a Deriv* in order to access the member in this way. Why are you making the pointer of type to the base class anyway? This is only usually useful when you have a polymorphic class hierarchy.

    You could consider using a virtual function to return the value of this member, but without knowing what your classes and code is trying to achieve it is hard to recommend one way or the other. Based on your small code sample, simply using

    Deriv *t = new Deriv(2);
    

    instead, seems a better choice

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

Sidebar

Related Questions

I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
On Visual Studio 2010 the following program #include <iostream> using std::cout; int main() {
If I have the following basic C++ program: #include <iostream> using namespace std; class
I have the following code #include <vector> #include <iostream> class A{ private: std::vector<int> x;
In C++ I have this program: #include <iostream> using namespace std; int main(){ string
I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following template: #include <iostream> template <class T,T defaultVal, int dim=255> class
I have the following program: #include <iostream> struct X { int a; float b;
I have the following program: #include <initializer_list> #include <iostream> class A; class nih {
I have the following code: #include stdafx.h #include <iostream> #include <conio.h> using namespace std;

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.