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

The Archive Base Latest Questions

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

I have to make a member data pointer to get less copy of the

  • 0

I have to make a member data pointer to get less copy of the data.I have writed some sample code.Some error occurred where I marked “error”.
How to do rightly.

#include <iostream>

class A {
public:
    A() {
        stra="12345";
    }
    virtual ~A() {}
    string stra;
}

class B {
public:
    B(A *pt) {
        strb=&(pt->stra);  //Error.An assignment statement
    }
    virtual ~B() {}
    string A::*strb;       //member data pointer from class A
}

int main() {
    A ma;
    B mb(&ma);
    std::cout<< *(mb.strb) <<std::endl;  //Error.print data
}
  • 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:54:17+00:00Added an answer on June 15, 2026 at 4:54 am

    In effect, what you have is a standard pointer, not a pointer-to-member. The following will work:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class A {
    public:
        A() {
            stra="12345";
        }
        virtual ~A() {}
        string stra;
    };
    
    class B {
    public:
        B(A *pt) {
            strb=&(pt->stra);
        }
        virtual ~B() {}
        string* strb; // <<<<<<<< THIS
    };
    
    int main() {
        A ma;
        B mb(&ma);
        std::cout<< *(mb.strb) <<std::endl;
    }
    

    For some discussion on pointers to data members, see C++: Pointer to class data member

    I don’t know where you’re going with all this, but having instances of one class keep pointers to things inside instances of another class strikes me as a rather poor design.

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

Sidebar

Related Questions

I have a member variable that in a class that I want to make
For example I have make test # do other stuff 'make test' runs some
Using this sample : I have make my own Fragment that holds tabhost and
In a project I'm writing code for, I have a void pointer, implementation, which
I have a class Message that has a std::string as a data member, defined
In Java, is it possible to override member data in a subclass and have
I have a page that I use AJAX to save some data in a
I have some data type which, if I were to use plain old C
I want to have a class with a private static data member: class C
error: invalid use of non-static data member ‘thread::tfun’ Class thread { typedef void* (th_fun)

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.