passing class pointer to other objects

Discussion in 'C++' started by Jeff, Apr 5, 2006.

  1. Jeff

    Jeff Guest

    Hello-

    1. I create a fully complete class (ListClass)

    2. I pass the pointer of this class to two (or more) other classes.

    3. I get a SEGV while accessing a member of the top level class
    (ListClass)

    Essentially the ListClass reads a file and fills its data fields.
    After its initialization
    I need to pass it to multiple other objects so that they have the data
    which is
    read only at this point (Im sure of this btw).

    Code snippet:

    ListClass *lc = new ListClass(filename);
    classA a(lc);
    classB b(lc); <====SEGV

    classA(ListClass *list) :
    m_val(list->getValue())
    {
    }

    classB(ListClass *list) :
    m_val(list->getValue()) <----real SEGV line
    {
    }

    int
    ListClass::getValue(int val) const
    {
    return m_value;
    }

    I had thought that I passed in a pointer to the ListClass class and
    could
    read anything I want without troubles. Can someone more experienced
    please give me some feedback? I have a feeling something is getting
    constructed instead of just passed, but IDK.

    Thanks,
    Jeff
    Jeff, Apr 5, 2006
    #1
    1. Advertising

  2. Jeff

    Gavin Deane Guest

    Jeff wrote:
    > Hello-
    >
    > 1. I create a fully complete class (ListClass)
    >
    > 2. I pass the pointer of this class to two (or more) other classes.
    >
    > 3. I get a SEGV while accessing a member of the top level class
    > (ListClass)
    >
    > Essentially the ListClass reads a file and fills its data fields.
    > After its initialization
    > I need to pass it to multiple other objects so that they have the data
    > which is
    > read only at this point (Im sure of this btw).
    >
    > Code snippet:


    Please don't post uncompileable hand-typed code snippets when you have
    a problem with code. Post a complete, minimal code example copied and
    pasted *directly* from your code editor into your message so that we
    can copy and paste directly from your message into an editor, compile
    it and be absolutely certain we are working with the same code as you.
    Minor differences can have major effects. See the FAQ

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    The reason for that is that without your exact code, I've had to guess
    what you might be doing. My guess is below. However, I don't see the
    problem you describe so my guess is probably wrong. That could be
    because the problem is with some code you *haven't* posted - which
    would make it kind of hard to fix. If my program works for you, post
    the actual program that is causing you problems, following the advice
    in the link above.

    > ListClass *lc = new ListClass(filename);


    Any reason why that isn't

    ListClass lc(filename);

    whatever filename is? Then pass &lc to the constructors for a and b.

    > classA a(lc);
    > classB b(lc); <====SEGV
    >
    > classA(ListClass *list) :
    > m_val(list->getValue())


    Here (and in the constructor for classB) you call ListClass::getValue
    with no parameters, but below the function takes an int. I had to fix
    that in my program just to get it to compile. Which should it be?

    > {
    > }
    >
    > classB(ListClass *list) :
    > m_val(list->getValue()) <----real SEGV line
    > {
    > }
    >
    > int
    > ListClass::getValue(int val) const
    > {
    > return m_value;
    > }
    >
    > I had thought that I passed in a pointer to the ListClass class and
    > could
    > read anything I want without troubles. Can someone more experienced
    > please give me some feedback? I have a feeling something is getting
    > constructed instead of just passed, but IDK.


    Here is my program using your code.

    #include <string>

    class ListClass
    {
    public:
    explicit ListClass(const std::string&) : m_value(0) {}
    int getValue() const;
    private:
    int m_value;
    };

    class classA
    {
    public:
    explicit classA(ListClass* list);
    private:
    int m_val;
    };

    class classB
    {
    public:
    explicit classB(ListClass* list);
    private:
    int m_val;
    };

    int main()
    {
    std::string filename;

    // Your code
    ListClass *lc = new ListClass(filename);
    classA a(lc);
    classB b(lc); //<====SEGV
    // End of your code
    }

    // More of your code
    classA::classA(ListClass *list) :
    m_val(list->getValue())
    {
    }

    classB::classB(ListClass *list) :
    m_val(list->getValue()) //<----real SEGV line
    {
    }

    int ListClass::getValue() const
    {
    return m_value;
    }

    Gavin Deane
    Gavin Deane, Apr 5, 2006
    #2
    1. Advertising

  3. Jeff

    Jeff Guest

    Thank you Gavin. I should have done so. As it turns out I made a very
    simple mistake and passed the wrong pointer to an object.
    Jeff, Apr 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James.D
    Replies:
    6
    Views:
    395
    James.D
    Mar 5, 2004
  2. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  3. 7stud
    Replies:
    11
    Views:
    669
    Dennis Lee Bieber
    Mar 20, 2007
  4. Pawel_Iks
    Replies:
    3
    Views:
    522
    terminator
    Jul 31, 2007
  5. ghoetker
    Replies:
    1
    Views:
    366
Loading...

Share This Page