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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:59:29+00:00 2026-06-13T12:59:29+00:00

I have a class and in there I have a struct which contains a

  • 0

I have a class and in there I have a struct which contains a string. In main I declare the class, then create a struct to reach that string.
How it looks like:

Class *myClass = new Class;
Class::Struct *myStruct;

myStruct = &myClass->getStruct(); // the struct is in a vector so I get it through a function

cout << myStruct->string << "\n";

So, with this method I can’t reach the string in the struct, but I can the int’s, float’s, etc.
If I change the code to this:

Class::Struct myStruct;

myStruct = myClass->getStruct();

cout << myStruct.string << "\n";

then it works.

My question is, why is the second one working, and the first not, and why only the string?
It’s not about life and death, because I don’t really need that string, I just tested the program, but I’m really curious what’s going on, because I’m still learning the language, and especially pointers.

Thanks in advance!

EDIT:
Ok, so the definitions

class Class {
public:
    struct Struct {
        string name;
        float data;
    };

    Class();
    Struct getStruct( int i );

private:
    vector<Struct>  tempStruct;
};

Class::Class() {
    Struct str;
    str.name = "test";
    str.data = 1.0f;

    tempStruct.push_back( str );
}

struct Class::getStruct( int i ) {
    return tempStruct[i];
}

EDIT2:
That was a little mistake, but I forgot the &. But you helped me to find it.

  • 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-13T12:59:30+00:00Added an answer on June 13, 2026 at 12:59 pm

    This is because getStruct() returns a copy of the struct. This is called a “temporary”. The temporary dies immediately. So now you have a pointer to a dead temporary. It doesn’t exist anymore so the pointer to it is invalid. Either return a pointer or reference to the struct in getStruct(), or hold a reference to its return value like this:

    const Class::Struct& myStruct = myClass->getStruct();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called UserInfo that contains details about a given user. There
In C++, I have a class which contains an anonymous bitfield struct. I want
We have class lua. In lua class there is a method registerFunc() which is
I have a class Field of which there are two sub-classes AccountField and DecimalField
I have a class called Path for which there are defined about 10 methods,
Is there a way to require that a class have a particular abstract member?
I have a Moose class that i would like to store using Apache::Session::File. However,
I'm making a Tic-tac-toe game and i have a board class which contains 9
I have a case where a struct may be preferable to a class: contains
I am having a struct defined in C++ which contains int and std::string arrays

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.