COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Inheritance related problem

• Email
• Follow

Note: this is *not* a ruby bug. its a general query.
Perhaps best explained with sample code.

#!/usr/bin/env ruby -w

class Parent
def printa arg
puts "inside parent printa #{arg} "
printb arg
end
def printb arg
puts "inside parent printb #{arg} "
puts "--> #{arg} "
end
end

class Child < Parent
def printa arg
puts "inside child printa #{arg} reduced 1 "
# 1 is actually some other instance level variable
super arg-1
end
def printb arg
puts "inside child printb #{arg}  reduced 1"
super arg-1
end
end

if __FILE__ == $0 begin p = Parent.new c = Child.new puts " parent calls with 5 and 6" p.printa 5 #p.printb 6 puts " === child calls with 7 === " c.printa 7 #c.printb 7 ensure end end Class Child extends Parent. It extends 2 methods by modifying the incoming parameter and then calling super. One of these methods printa calls printb. When Parent's printa is called, it calls it own printb. However, when Child's printa is called, it calls Parent's printa, which (in this case) i was hoping would call Parent's printb directly. But it (correctly) calls Child's printa which once again reduces the arg. So my arg gets reduced twice. I suppose i could use some flags to prevent this, But is there any direct way i can coerce Parent printa to call only Parent printb. When you execute the above code, you will see that 7 gets decremented 2 times. Attachments: http://www.ruby-forum.com/attachment/4448/test.rb -- Posted via http://www.ruby-forum.com/.   0 Reply R 1/31/2010 7:44:36 AM See related articles to this posting Hi, I'm going to give you a meta-answer to the problem... If the two printb routines do the same thing, then child printb is redundant. Remove it. If they do different things, then they should have different names - problem avoided. I don't know enough ruby to answer your specific problem. Regards Ian R. Kumar wrote: > Note: this is *not* a ruby bug. its a general query. > Perhaps best explained with sample code. > > #!/usr/bin/env ruby -w > > class Parent > def printa arg > puts "inside parent printa #{arg} " > printb arg > end > def printb arg > puts "inside parent printb #{arg} " > puts "--> #{arg} " > end > end > > class Child < Parent > def printa arg > puts "inside child printa #{arg} reduced 1 " > # 1 is actually some other instance level variable > super arg-1 > end > def printb arg > puts "inside child printb #{arg} reduced 1" > super arg-1 > end > end > > if __FILE__ ==$0
>   begin
>       p = Parent.new
>       c = Child.new
>       puts " parent calls with 5 and 6"
>       p.printa 5
>       #p.printb 6
>       puts " === child calls with 7 === "
>       c.printa 7
>       #c.printb 7
>   ensure
>   end
> end
>
>
> Class Child extends Parent. It extends 2 methods by modifying the
> incoming parameter and then calling super. One of these methods printa
> calls printb.
>
> When Parent's printa is called, it calls it own printb.
> However, when Child's printa is called, it calls Parent's printa, which
> (in this case) i was hoping would call Parent's printb directly. But it
> (correctly) calls Child's printa which once again reduces the arg.
>
> So my arg gets reduced twice. I suppose i could use some flags to
> prevent this, But is there any direct way i can coerce Parent printa to
> call only Parent printb.
>
> When you execute the above code, you will see that 7 gets decremented 2
> times.
>
> Attachments:
> http://www.ruby-forum.com/attachment/4448/test.rb
>
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 9.0.733 / Virus Database: 271.1.1/2657 - Release Date: 01/30/10 07:35:00
>
>


 0

Ian Hobson wrote:
> Hi,
>
> I'm going to give you a meta-answer to the problem...
>
> If the two printb routines do the same thing, then child printb is
> redundant. Remove it.
>
> If they do different things, then they should have different names -
> problem avoided.
>
>
> Regards
>
> Ian

They need to have the same name since Child extends the functionality of
Parent. Both classes will be used in the same situation. That's why
Child extends Parent.
--
Posted via http://www.ruby-forum.com/.


 0

> When Parent's printa is called, it calls its own printb.
> However, when Child's printa is called, it calls Parent's printa, which
> (in this case) i was hoping would call Parent's printb directly. But it
> (correctly) calls Child's printa which once again reduces the arg.
>
> So my arg gets reduced twice. I suppose i could use some flags to
> prevent this, But is there any direct way i can coerce Parent printa to
> call only Parent printb.

I'm having trouble understanding how you got into this position.  I'm
assuming that you don't own Parent, and so have to work your magic in
Child.  For the specific example you described, the following will
work...

Change Child#printa to:
def printa arg

# The following block gets evaluated with Child as self
child_print_b = Child.class_eval do

# Save the Child method
print_b = instance_method(:printb)

# Remove it from the class
remove_method :printb

# Return it to the function for later restoration
print_b
end

puts "inside child printa #{arg} reduced 1 "
# 1 is actually some other instance level variable

# This call will now eventually call Parent.printb
# because there is no Child.printb
super arg-1

# Restore the original printb
Child.class_eval do
define_method :printb, child_print_b
end
end

Outputs:
parent calls with 5
inside parent printa 5
inside parent printb 5
--> 5
=== child calls with 7 ===
inside child printa 7 reduced 1
inside parent printa 6
inside parent printb 6 <-------- Parent::printa called from
Child::printa calls Parent::printb
--> 6
inside child printb 7  reduced 1
inside parent printb 6
--> 6

 0

Sean DeNigris wrote:
>> When Parent's printa is called, it calls its own printb.
>> However, when Child's printa is called, it calls Parent's printa, which
>> (in this case) i was hoping would call Parent's printb directly. But it
>> (correctly) calls Child's printa which once again reduces the arg.
>>
>> So my arg gets reduced twice. I suppose i could use some flags to
>> prevent this, But is there any direct way i can coerce Parent printa to
>> call only Parent printb.
>
> I'm having trouble understanding how you got into this position.  I'm
> assuming that you don't own Parent, and so have to work your magic in
> Child.  For the specific example you described, the following will
> work...
>

I own both. Parent is a wrapper around ncurses Window. Child is a
wrapper around ncurses Pad. However, they are used in a similar manner.
So in the original app, i used only Window. Now in many cases I use Pad.
They both should work interchangeably since they essentially do the same
thing but often in different ways. An object could use Window, or Pad.

However, stuff like printing a border or writing a string onto the
window are essentially the same except that the offsets in Window are
absolute whereas the Pad offsets are relative. So when Pad calls super
it only has to make the relative offsets absolute:

def printstring(row, col, value, color, attrib=Ncurses::A_NORMAL)
super(row - @top, col - @left, value, color, attrib)
end # printstring

def print_border row, col, height, width, color,
att=Ncurses::A_NORMAL
super(row - @top, col - @left, height, width,  color, att)
end

Now in the parent Window, print_border does a call to printstring to
reset the background. Unfortunately, the child's printstring gets called
and @top and @left get decremented again.

I looked at your workaround -- a bit complicated, and could slow things
down since display has to be fast. I can use a variable that I set and
check, however, is there some direct way in ruby. I know the answer is
most likely NO, but just thought i'd ask in case i've overlooked
something.
--
Posted via http://www.ruby-forum.com/.


 0

On Mon, Feb 1, 2010 at 1:54 AM, R. Kumar <sentinel.2001@gmx.com> wrote:
> Sean DeNigris wrote:
>>> When Parent's printa is called, it calls its own printb.
>>> However, when Child's printa is called, it calls Parent's printa, which
>>> (in this case) i was hoping would call Parent's printb directly. But it
>>> (correctly) calls Child's printa which once again reduces the arg.
>>>
>>> So my arg gets reduced twice. I suppose i could use some flags to
>>> prevent this, But is there any direct way i can coerce Parent printa to
>>> call only Parent printb.
>>
>> I'm having trouble understanding how you got into this position. =A0I'm
>> assuming that you don't own Parent, and so have to work your magic in
>> Child. =A0For the specific example you described, the following will
>> work...
>>
>
> I own both. Parent is a wrapper around ncurses Window. Child is a
> wrapper around ncurses Pad. However, they are used in a similar manner.
> So in the original app, i used only Window. Now in many cases I use Pad.
> They both should work interchangeably since they essentially do the same
> thing but often in different ways. An object could use Window, or Pad.
>
> However, stuff like printing a border or writing a string onto the
> window are essentially the same except that the offsets in Window are
> absolute whereas the Pad offsets are relative. So when Pad calls super
> it only has to make the relative offsets absolute:
>
> =A0 =A0def printstring(row, col, value, color, attrib=3DNcurses::A_NORMAL=
)
> =A0 =A0 =A0super(row - @top, col - @left, value, color, attrib)
> =A0 =A0end # printstring
>
> =A0 =A0def print_border row, col, height, width, color,
> att=3DNcurses::A_NORMAL
> =A0 =A0 =A0super(row - @top, col - @left, height, width, =A0color, att)
> =A0 =A0end
>
> Now in the parent Window, print_border does a call to printstring to
> reset the background. Unfortunately, the child's printstring gets called
> and @top and @left get decremented again.

So why not just factor out what's different between Parent and Child

class Parent
def printstring(row, col, ... other parameters omitted)
row, col =3D *transform(row, col)
# do whatever you need
end

def transform(row, col)
[row, col]
end
end

class Child < Parent
def transform(row, col)
[row - @top, col - @left]
end
end

--=20
Rick DeNatale

WWR: http://www.workingwithrails.com/person/9021-rick-denatale


 0

Just to be clear I meant to say why not just factor out JUST what's differe=
nt.

On Mon, Feb 1, 2010 at 8:59 AM, Rick DeNatale <rick.denatale@gmail.com> wro=
te:
> On Mon, Feb 1, 2010 at 1:54 AM, R. Kumar <sentinel.2001@gmx.com> wrote:
>> Sean DeNigris wrote:
>>>> When Parent's printa is called, it calls its own printb.
>>>> However, when Child's printa is called, it calls Parent's printa, whic=
h
>>>> (in this case) i was hoping would call Parent's printb directly. But i=
t
>>>> (correctly) calls Child's printa which once again reduces the arg.
>>>>
>>>> So my arg gets reduced twice. I suppose i could use some flags to
>>>> prevent this, But is there any direct way i can coerce Parent printa t=
o
>>>> call only Parent printb.
>>>
>>> I'm having trouble understanding how you got into this position. =A0I'm
>>> assuming that you don't own Parent, and so have to work your magic in
>>> Child. =A0For the specific example you described, the following will
>>> work...
>>>
>>
>> I own both. Parent is a wrapper around ncurses Window. Child is a
>> wrapper around ncurses Pad. However, they are used in a similar manner.
>> So in the original app, i used only Window. Now in many cases I use Pad.
>> They both should work interchangeably since they essentially do the same
>> thing but often in different ways. An object could use Window, or Pad.
>>
>> However, stuff like printing a border or writing a string onto the
>> window are essentially the same except that the offsets in Window are
>> absolute whereas the Pad offsets are relative. So when Pad calls super
>> it only has to make the relative offsets absolute:
>>
>> =A0 =A0def printstring(row, col, value, color, attrib=3DNcurses::A_NORMA=
L)
>> =A0 =A0 =A0super(row - @top, col - @left, value, color, attrib)
>> =A0 =A0end # printstring
>>
>> =A0 =A0def print_border row, col, height, width, color,
>> att=3DNcurses::A_NORMAL
>> =A0 =A0 =A0super(row - @top, col - @left, height, width, =A0color, att)
>> =A0 =A0end
>>
>> Now in the parent Window, print_border does a call to printstring to
>> reset the background. Unfortunately, the child's printstring gets called
>> and @top and @left get decremented again.
>
> So why not just factor out what's different between Parent and Child
>
> class Parent
> =A0def printstring(row, col, ... other parameters omitted)
> =A0 =A0 =A0row, col =3D *transform(row, col)
> =A0 =A0 =A0# do whatever you need
> =A0end
>
> =A0def transform(row, col)
> =A0 =A0 [row, col]
> =A0end
> end
>
> class Child < Parent
> =A0 def transform(row, col)
> =A0 =A0 =A0 [row - @top, col - @left]
> =A0 end
> end
>
> --
> Rick DeNatale
>
> WWR: http://www.workingwithrails.com/person/9021-rick-denatale
>
>

--=20
Rick DeNatale

WWR: http://www.workingwithrails.com/person/9021-rick-denatale


 0

This is probably the same as what Rick was saying...

If you own Parent, then the method I showed is definitely not what you
want to do.  And, the solution does not require a special Ruby way,
just a refactor:
class Parent
def printa arg
puts "inside parent printa #{arg} "
>     task_b arg #<-- problem solved, Parent#printa will always use the same version of printb
end

def printb arg
>   task_b arg #<-- delegate work to an extracted method
end

>private
> def task_b arg # Original implementation of Parent#printb is private helper method
>   puts "inside parent printb #{arg} "
>   puts "--> #{arg} "
> end
end

Output:
parent calls with 5 and 6
inside parent printa 5
inside parent printb 5
--> 5
=== child calls with 7 ===
inside child printa 7 reduced 1
inside parent printa 6
inside parent printb 6
--> 6

 0

Rick Denatale wrote:
> On Mon, Feb 1, 2010 at 1:54 AM, R. Kumar <sentinel.2001@gmx.com> wrote:
>>> I'm having trouble understanding how you got into this position. �I'm
>>
>> att=Ncurses::A_NORMAL
>> � � �super(row - @top, col - @left, height, width, �color, att)
>> � �end
>>
>> Now in the parent Window, print_border does a call to printstring to
>> reset the background. Unfortunately, the child's printstring gets called
>> and @top and @left get decremented again.
>
> So why not just factor out what's different between Parent and Child
>
> class Parent
>   def printstring(row, col, ... other parameters omitted)
>       row, col = *transform(row, col)
>       # do whatever you need
>   end
>
>   def transform(row, col)
>      [row, col]
>   end
> end
>
> class Child < Parent
>    def transform(row, col)
>        [row - @top, col - @left]
>    end
> end
>

Aah, there are more such methods.

SO in the second method print_border which happens to call printstring I
would save row, col for the next call.

> class Parent
>   def print_border(row, col, ... other parameters omitted)
>-       row, col = *transform(row, col)
>+       nrow, ncol = *transform(row, col)
>       # do whatever you need with nrow and ncol
>+       printstring row, col # using original args
>   end

Looks good, will give it a try. I feel there's still something "off" in
what i've done, and the question I am asking you, since it requires the
parent to protect itself from (or know of) misbehaviour from a child.

The overriding in Child was tacked on hastily and worked great till i
noticed some cases where an extra line was getting erased.

Thanks a lot.
--
Posted via http://www.ruby-forum.com/.


 0

On Mon, Feb 1, 2010 at 10:10 AM, R. Kumar <sentinel.2001@gmx.com> wrote:
> Looks good, will give it a try. I feel there's still something "off" in
> what i've done, and the question I am asking you, since it requires the
> parent to protect itself from (or know of) misbehaviour from a child.

Inheritance always introduces coupling between class and superclass.
The answer is to cover the code with tests, and run them in order to
ensure that the code works correctly when developed, and then to
detect any regressions.

The other think to watch out for in this particular case, is not to
confuse the implementation relationship of subclassing/module
inclusion with the containment relationship of windows/widgets.  You
don't seem to be doing this from what you've posted, but ...

--
Rick DeNatale

WWR: http://www.workingwithrails.com/person/9021-rick-denatale


 0

Rick Denatale wrote:

>
> The other think to watch out for in this particular case, is not to
> confuse the implementation relationship of subclassing/module
> inclusion with the containment relationship of windows/widgets.  You
> don't seem to be doing this from what you've posted, but ...
>
Not sure what you mean by what I've posted - do you mean the sample code
which was only illustrative since the actual code will not be runnable.
Or what i have later discussed... since in my case a Pad is a window and
is not contained in a window.
Where a Window was being used earlier, now a Pad may be used. However,
the user objects are unaware of whether they are writing to a Window or

constructor of Window creates a ncurses window, whereas the constructor
super. However, by subclassing i get all the behaviour of Window which
is essentially identical.

Sadly, i can't cover this with automated tests since its all visual and
the most errors can only be seen visually.
--
Posted via http://www.ruby-forum.com/.


 0

Sean DeNigris wrote:
> This is probably the same as what Rick was saying...
>
> If you own Parent, then the method I showed is definitely not what you
> want to do.  And, the solution does not require a special Ruby way,
> just a refactor:
> class Parent
>   def printa arg
>       puts "inside parent printa #{arg} "
>>     task_b arg #<-- problem solved, Parent#printa will always use the same version of printb
>   end
>
>   def printb arg
>>   task_b arg #<-- delegate work to an extracted method
>   end

Ok, i get it, printa should not call printb since that goes thru the
inheritance chain. They should both call task_b in which the work has
been factored.

Thanks !!!
--
Posted via http://www.ruby-forum.com/.


 0

2010/1/31 R. Kumar <sentinel.2001@gmx.com>:
> [...]
> So my arg gets reduced twice. I suppose i could use some flags to
> prevent this, But is there any direct way i can coerce Parent printa to
> call only Parent printb.

I don't know if that's what the original post meant, but I'm
interested in knowing how to specify the exact class which method is
called when inheritance is involved.

Example (C++):
class Father
{
virtual void m();
}

class Child
{
void m();
void aM() {Father::m();}
}

This forces the call to the method "m" of the Father class, rather
than using Child's. Is this possible in Ruby; if yes, how ? If
no...why ? :-(

TIA,

--
Xavier NOELLE


 0

Sean DeNigris wrote:
> This is probably the same as what Rick was saying...
>
> If you own Parent, then the method I showed is definitely not what you
> want to do.  And, the solution does not require a special Ruby way,
> just a refactor:

**Thanks**, all. I did a refactor of printstring() and it worked
straight off. Very clean.
--
rkumar
--
Posted via http://www.ruby-forum.com/.


 0
Reply sentinel.2001 (154) 2/5/2010 3:07:27 PM

> Example (C++):
> class Father
> {
>   virtual void m();
> }
>
> class Child
> {
>    void m();
>    void aM() {Father::m();}
> }
>
> This forces the call to the method "m" of the Father class, rather
> than using Child's. Is this possible in Ruby; if yes, how ? If
> no...why ? :-(
>
> TIA,

If you are saying that aM() forces a call to the parent's m() and of
course you have inherited from Father, then yes.

def aM
super.m
end
--
Posted via http://www.ruby-forum.com/.


 0
Reply sentinel.2001 (154) 2/5/2010 3:10:22 PM

2010/2/5 R. Kumar <sentinel.2001@gmx.com>:
> If you are saying that aM() forces a call to the parent's m() and of
> course you have inherited from Father, then yes.

Yes, my mistake, I forgot the "public Father".

> def aM
> =A0super.m
> end

Yes, indeed, but what if I need to call a specific parent's method ?
Say Parent > Child > Grandchild. Is it possible to Parent::m in
Grandchild (other than super.super.m(), if it works :-)) ?

--=20
Xavier NOELLE


 0

> def aM
> =A0 super.m
> end

Am I missing something?  "super" calls the method with the same name
in the superclass.  So here, it calls Child#aM (which doesn't exist in
this example), then m would be called on whatever that call returned

class Parent
def a
puts "parent a"
end
end

class Child < Parent
def a
puts "child a"
end
end

class Grandchild < Child
def a
puts "grandchild a"
end

def grandpa_a
super.a
end
end

g =3D Grandchild.new.grandpa_a

Output:
test.rb:19:in grandpa_a': super: no superclass method
grandpa_a' (NoMethodError)

I can't think of a way to do what you want except by the methods
suggested previously.

Sean

 0
Reply sean952 (6) 2/5/2010 4:30:40 PM

Xavier Noëlle wrote:
> 2010/2/5 R. Kumar <sentinel.2001@gmx.com>:
>> If you are saying that aM() forces a call to the parent's m() and of
>> course you have inherited from Father, then yes.
>
> Yes, my mistake, I forgot the "public Father".
>
>> def aM
>> �super.m
>> end
>
> Yes, indeed, but what if I need to call a specific parent's method ?
> Say Parent > Child > Grandchild. Is it possible to Parent::m in
> Grandchild (other than super.super.m(), if it works :-)) ?

I doubt that that's possible in the way you want, since ruby is not
strictly typed, so this kind of type-casting won't work.
However, there could be ways using metadata- you'll have to wait for an

Simple way:
put an explicit method in class such as grandpa_a or parent_a for the
one you want to call.
You could reopen the class and add the method where required.

Others:
Since you can get public methods of an object, i think you can go up the
heirarchy and get the specific method. And then pass your message to it
using send.
You could do a one time traversal and store a ref to the method.

Surely there will be other methods.
--
Posted via http://www.ruby-forum.com/.


 0
Reply sentinel.2001 (154) 2/5/2010 5:19:42 PM

On Feb 5, 2010, at 10:33 AM, Xavier No=EBlle wrote:

> 2010/2/5 R. Kumar <sentinel.2001@gmx.com>:
>> If you are saying that aM() forces a call to the parent's m() and of
>> course you have inherited from Father, then yes.
>=20
> Yes, my mistake, I forgot the "public Father".
>=20
>> def aM
>>  super.m
>> end
>=20
> Yes, indeed, but what if I need to call a specific parent's method ?
> Say Parent > Child > Grandchild. Is it possible to Parent::m in
> Grandchild (other than super.super.m(), if it works :-)) ?

That idiom isn't well supported in Ruby, but it is possible
in an awkward sort of way:

$cat sample.rb class Parent def foo(*args) puts "Parent:foo: #{args.inspect}" end end class Child < Parent def foo(*args) super puts "Child:foo: #{args.inspect}" end def parent_foo(*args) Parent.instance_method('foo').bind(self).call(*args) end end$ irb
=3D> true
>> Parent.new.foo(1,2,3)
Parent:foo: [1, 2, 3]
=3D> nil
>> Child.new.foo(4,5,6)
Parent:foo: [4, 5, 6]
Child:foo: [4, 5, 6]
=3D> nil
>> Child.new.parent_foo(7,8,9)
Parent:foo: [7, 8, 9]
=3D> nil
>>=20

Gary Wright


 0
Reply gwtmp01 (829) 2/5/2010 5:59:23 PM

Xavier Noëlle wrote:
> Yes, indeed, but what if I need to call a specific parent's method ?
> Say Parent > Child > Grandchild. Is it possible to Parent::m in
> Grandchild (other than super.super.m(), if it works :-)) ?

Besides the solution Gary Wright has shown, there's a simple solution:

class Parent
def blah
puts 'blah blah blah'
end
alias original_blah blah
end

Now, if some class inherits and overrides blah(), you can still call the
original blah() by doing original_blah().

--
Posted via http://www.ruby-forum.com/.


 0
Reply albertschlef (195) 2/6/2010 11:24:50 PM

19 Replies
118 Views

Similar Articles

12/12/2013 10:12:11 AM
[PageSpeed]

Similar Artilces:

Multiple inheritance problem.
I have a simple code which contains multiple inheritance. I am bit confused with the output. Can anyone please tell me why its behaving this way? class Base { public: Base() { cout << "I m in Base\n"; } Base(const string& str) { cout << "I m in Base string with " << str << "\n"; } Base( const Base& b) { cout << "I m in Base copy const\n"; } }; class Derived1 : virtual public Base { public: Derived1() { cout << "I m in Deriv...

problem related to transferhandler
i have gone through the examples provided by java help but could not find any solution related to my problem..! Problem: If i am extending the TransferHandler how do i implement the export method for exporting the data of my class type? Eg:ElementWrapper exportElement(JComponent c); the sun people do have given an example related to string but need to find out realted to data other then string line element of dom4j if any body has imlemented export method for different data type other then string please help me out..! waiting for all u experts reaply..! On 27 Oct 2005 01:14:40 -0700, "P...

Relative Filename Problem
Hi: I have a problem regarding filenames in Visual C++. I would like to read from files using relative filenames instead of absolute filenames. I have tried using the following fname = .\\<data directory>>\\<<file1>> I have no problem when executing from within the IDE (i.e. pressing CTRL+F5) but when I run the executable (i.e. <prog.exe> ), the file cannot be found. When I tried to change it to fname = ../<data directory>>/<<file1>> the executable can work but this time round, executing from the IDE will render file not found problem. ...

Relative import problem
Hi all, I want to structure my app so that I have two dirs like; obj/{object files} gui/{gui files} Here comes the catch. From the GUI dir, I would like to access the obj submodule path. I need to go one dir back.. I read there was something like from .. import x in python 2.5 so that I could access my obj dir from a lower level, but I have problems getting this to work. Example; In the main dir I have; main.py: -------- import gui gui.gui.startme() -------- In the obj dir I have; obj/obj.py --------- def start(): print 'Started OBJ' --------- In the GUI dir I have gui...

Design problem with inheritance
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); } class intABC : public ABC { public : virtual void operation1(); virtual void operation2(); void SetValue(int); int GetValue(); private: int val; } class charABC : public ABC { public : virtual void operation1(); virtual void operation2(); void SetValue(char); char GetValue(); private : ch...

Template Inheritance Problem
I have following code: #include <iostream> using namespace std; template <class T> class Parent { public: void fun(const T a){ } }; class myclass: public Parent<int*> { public: void myclassfun(const int* a) { Parent<int*>::fun(a); } }; int main() { myclass obj1; int * ptr; obj1.myclassfun(ptr); return 0; } This gives me compilation error: "In member function void myclass::myclassfun(const int*)': error: invalid conversion from const int*' to int*' error: initializing argument 1 of void Parent<T>::f...

Diamon inheritance problem
hi, can diamond inhertance problem be solved using virtual functions,, or can only be done with scope resolution operators.. is there any other way to solve this problem... Thanks, suri surendran.d@gmail.com wrote: > hi, > > can diamond inhertance problem be solved using virtual functions,, or > can only be done with scope resolution operators.. is there any other > way to solve this problem... > Virtual base classes. http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 and others in the same section might be useful * surendran.d@gmail.com: > &...

sound related problem
Hi!, I installed the sound card Yamaha YMF PCI, the modules look like correctly loaded: ------------------------------------------------------------ Module Size Used by Tainted: P uhci 24444 0 (unused) usbcore 59308 1 [uhci] snd-ymfpci 42852 0 (unused) gameport 1420 0 [snd-ymfpci] snd-pcm 56072 0 [snd-ymfpci] snd-page-alloc 6328 0 [snd-ymfpci snd-pcm] snd-opl3-lib 5892 0 [snd-ymfpci] snd-hwdep 4804 0 [snd-opl3-lib] snd-timer ...

problem related to printf!! why is it so???
It is a problem related to printf, and buffering to the printf statements Look through this program. main() { int pid; int loop,max=3; for(loop = 0; loop <max;loop++) { pid = fork(); if(pid < 0) printf("\nThe greatest fork error of the decade"); else if(pid==0) { printf("\nI am a child"); goto out; } else { printf("\nI have grown up"); } } out: } output: I am a child I have grown up I am a childI have grown up I have grown up...

Design problem with inheritance
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); } class intABC : public ABC { public : virtual void operation1(); virtual void operation2(); void SetValue(int); int GetValue(); private: int val; } class charABC : public ABC { public : virtual void operation1(); virtual void operation2(); void SetValue(char); char GetValue(); private : ch...

Classes inheritance problem
Hi everybody. I've discovered the following problem while building an APP: /* Code Start **************************/ // Definition function cClass_prototype_prototype() { this.array = new Array; this.className = "cClass_prototype_prototype"; } function cClass_prototype() { this.className = "cClass_prototype"; } cClass_prototype.prototype = new cClass_prototype_prototype; function cClass1() { this.className = "cClass1"; } function cClass2() { this.className = "cClass2"; } cClass1.prototype = new cClass_prototyp...

Hyperref related problem
Hi, I have a report-class TeX document, which has (among other packages) the hyperref and a university-defined thesis package (ncsu-thesis.sty). My problem is: ONLY when I use the colorlinks option in the hyperref package, I get the error ======================= ! Too many }'s. \color@endbox -> \color@endgroup \egroup 1.116 \end{table} ======================= Also, if it is of any help, I use pdfLaTeX to typeset. The TeX compilation stops at the first occurence of a Table or Figure. Now, if I don't include the ncsu-thesis.sty file, I have no problems. I read thru' several po...

Problems with prototype inheritance
Hi, In my research in the javascript language I have encountered problems with implementing prototype inheritance while preserving private methods functioning properly. Here is an example: function A(){ var _this = this; this.a = 10; var privateFunc = function(){ alert(_this.a) } this.publicFunc = function(){ this.a = 20; privateFunc(); } } function B(){ this.publicFunc(); } B.prototype = new A(); b = new B(); The expected popup window produced from the following code (alert) should show 20. Yet it shows 10. Here we have 2 Class (or rather constructor functions): A, B; ----...

Relative view problem
Hi All I have a multi body part, I have created a drawing view using the relative view option and selected about half the bodies, added projected views and annotations. Now I want to add one of the other bodies to the same view, can't find a way. I could create configurations or split the part or remake the view but this should be possible. Steve ...

object oriented inheritance problem
I am trying to inherit from ElementTree so I can add some methods. This is the code I am trying to make work, and the following is the error I am getting. from elementtree import ElementTree class AcidTree(ElementTree): def write_string(self): .... File "/home/hope/var/proj/acid/server/mgacparse.py", line 22, in ? class AcidTree(ElementTree): TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) I have *no* idea what is going on here. AcidTree does not implement an __init__. It just adds two simple methods, whi...

problem with relative paths for references
I have a problem with setting references between library databases when I move them to a different location. I thought that as long as the relative path remained constant, the references would remain intact, but this doesn't seem to be the case. I have this sort of structure, with several Library databases in each subdirectory: DBDir+---LibSubDir1 | +---LibSubDir2 References between libraries in LibSubDir1 cope fine when the DBDir and contents are move to another location, but references from libraries in LibSubdir1 to those in LibSubDir2 break. I thought that, since the relat...

a problem relate to namespace and using
Hello, I want to know whether the following program is legal. the g++-4.1 can not compile it $g++ -c bug2.cpp bug2.cpp:8: error: expected unqualified-id before '}' token bug2.cpp:15: error: 'A' is already declared in this scope bug2.cpp:20: error: prototype for 'void n1::B::foo(const n1::n2::A&)' does not match any in class 'n1::B' bug2.cpp:6: error: candidate is: void n1::B::foo(const n1::A&)$ // begin bug2.cpp // #include "b.hpp" namespace n1 { class A; class B { void foo(const A&); } } // #include "a.hpp" namespa...

Newbie problem with relating tables
I have 2 tables, service_user and qualifying_postcode. I have a postcode field in the service_user table (su_postcode_full), and I need to check whether the postcode exists in the qualifying_postcode table. If it does exist, I need to know whether it's a "type n" postcode or a "type d" postcode. I had hoped to relate the 2 tables by postcode. So that if there's a match, I just obtain pc_type from the related qualifying_postcode rec . If there's no match, there's no related pc_type, and the postcode doesn't qualify. But I *can't* relate the tabl...

Problem with OJB and Inheritance hierarcy
Hi, I am using OJB for the first time. I have three tables, say T1, T2 and T3. These tables are the ER representation of three java classes C1, C2 and C3. From the object orientation point of view C1 is the super class of the other two. When I had to map the classes with the tables I've used the "table per subclass" method (map every subclass to a distinct table). The problem is : when I try to delete an Object of class C2 I expect that OJB delete the C2 object and even the referenced C1 object. But this does not happens. How could I achieve this behaviour? Tha...

Iterator Related Design Problem
Hi. I'm looking for suggestions to this design problem: I have an unsorted list of Items. Let's say the list is called called ItemList. I need to find the item with the highest priority that also meets some other criteria. Let's say that other criteria is called criteria X. Someone suggested that we approach this by doing the following: 1- Make a copy of the ItemList (actually a list of references to the Items on the first list, i.e., a shallow copy). 2- Sort this copy based on Item priority. 3- Sequentially search the prioritized list for the first Item to meet criteria X...