passing class pointer to other objects

J

Jeff

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
 
G

Gavin Deane

Jeff said:
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
 
J

Jeff

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top