Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing class pointer to other objects

Reply
Thread Tools

passing class pointer to other objects

 
 
Jeff
Guest
Posts: n/a
 
      04-05-2006
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

 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      04-05-2006

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-lit...t.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

 
Reply With Quote
 
 
 
 
Jeff
Guest
Posts: n/a
 
      04-05-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data between objects and calling all objects of a class in turn ghoetker Python 1 08-25-2010 03:18 AM
how to call method of the class which contains a pointer to other class method? Pawel_Iks C++ 3 07-31-2007 06:30 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
A pointer in a class points to an other member function in the same class? James.D C++ 6 03-05-2004 02:57 AM



Advertisments