Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi
I have a problem
I have a base class :
 
class Base
{
public:
    myClass* a;
    virtual void Update(){}
 
};
and another class :
 
class NewClass:public Base
{
public:
    NewClass();
    virtual void Update()
    {
        a->val=200;
    }
}
 
now I do this :
    myClass* b;
    Base me;
    me=NewClass();
    me.a=b;
    me.Update();
 
but now b dont change. I want change b value. how can I do this like this ?
Posted 30-Dec-12 8:45am
Edited 30-Dec-12 22:37pm
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I haven't tried it but I wouldn't expect your code to compile.
 
You are describing 3 classes here: Base, NewClass and myClass. Base includes a pointer to myClass, which in your code as provided, is not defined (error). If you somehow overlook this and define it somehow, in the lower section of code:
 
1    myClass* b;
2    Base me;
3    me=NewClass();
4    me.a=b;
5    me.Update();
 
Lines 2 and 3 could be combined as
 
2/3    Base me=NewClass();
 
which represents use of a copy constructor, building a new 'Base' object, copying elements from a new 'NewClass' temporary object. At this point, me.a is undefined (usually trash from the stack with most compilers). Line 4 should assign me.a the value of b (on the stack) and line 5 will change b->val.
 
Assumptions:
(1) We are talking C++ here. Java for example does something different.
(2) This code segment is external to the classes in question.
(3) myClass contains something named 'val' (which is well formed POD and not a property or something else funky).
 
You didn't provide the code for class myClass, which could change all of the above.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I am sorry, but you have confused the usage of pointers
 
a=200; // is wrong
 
you probably want to do
 
*a = 200;
  Permalink  
v3
Comments
mohammadali1375 at 31-Dec-12 4:37am
   
Oh I'm sorry. I try make an example from my problem quickly . Now I edit my question;
Mattias Högström at 31-Dec-12 10:25am
   
The code didn't become clearer.
 
Theoretically, the assignment looks ok now,
but your code contains a couple of errors and omissions.
I don't think it compiles, so I am not sure what the error is.
 
1.
Allocate myClass object or use an object.
myClass* b = new myClass();
myClass b;
 
2.
When using subclasses and virtual methods, a pointer must be used.
Base me;
me=NewClass();
=== Change to ===>
Base *me = newClass();
 
3.
Exposing a public variable is bad practise, and of pointer type, is worse.
If you really need to pass around pointers, use the constructor and/or get set methods.
The reference type (myClass& par) is even better than pointers because it can't be null.
A "smart pointer" is even better to use than raw pointers.
http://en.wikipedia.org/wiki/Smart_pointer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 248
3 Sergey Alexandrovich Kryukov 243
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100