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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:06:24+00:00 2026-06-12T09:06:24+00:00

this may be an amateur question, but here it goes. I have three clases:

  • 0

this may be an amateur question, but here it goes. I have three clases:

DrawableObject:

class DrawableObject
{
private:

    int x;
    //...
public:
    int getX();
        //...
}

FormElement which inheirts from DrawableObject:

class FormElement : public DrawableObject

FormElement has a method called wasPushed:

bool FormElement::wasPushed(SDL_Event event)
{
bool wasPushed =
        (
            ( event.motion.x >= getX()) //Inherited getX()
            && // Blah blah...
        ) ? true : false;

return wasPushed;
}

Finally, TextField, which inheirts from FormElement:

class TextField : public DrawableObject

I also have a class, named Form:

class Form {

public:

    list<FormElement*> getFormElements();
    void generateForm();

private:

    list<FormElement*> formElements;
}

Form adds some TextFields to its list, in its generateForm() method:

void Form::generateForm() {

TextField *aTextField = new TextField(10, 10, 120);
    this->formElements.push_back(aTextField);
}

Later, it tries to iterate it:

for(list<FormElement*>::iterator it = getFormElements().begin()
    ; it != getFormElements().end()
    ; ++it)
        {
          if ( (*it)->wasPushed(theEvent) )
            { //Etc.

Well, the program exits, when it tries to access getX() from wasPushed method.

Could anyone please tell me why? What am I defining wrong?

I thank you very much.
Martín.

  • 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-12T09:06:25+00:00Added an answer on June 12, 2026 at 9:06 am

    You are returning the list by value:

    list<FormElement*> getFormElements();
    

    it should be by reference:

    list<FormElement*> &getFormElements();
    

    When you return by value, you are getting a temporary copy of the list.

    So in this code:

    for(list<FormElement*>::iterator it = getFormElements().begin()
        ; it != getFormElements().end()
    

    Your begin and end iterators are pointing to two different copies of the list. Also, those temporary copies will be destroyed before you ever have a chance to iterate over them.

    You could also use the formElements member directly:

    for(list<FormElement*>::iterator it = formElements.begin()
        ; it != formElements.end()
        ; ++it)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a very generic question but here it goes. I have
I know that this may be an amateur question but for some reason I
This may be a stupid question but I have a code with the following
This may be have a better name than custom tab completion, but here's the
This may be a simple question but I can;t find the answer anywhere. Here
This may be a silly question (but I'm new to multicast). I have a
This may be more of an OOP concept question, but here's what I'd like
This may be an unusual question, but are there any resources people have come
this may sound like a strange question from a Python noob, but here's the
This may sound like a silly question but I have created a single VIEW

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.