Re: Python to C++ translation?

2005-08-07 Thread Jorgen Grahn
On 18 Jul 2005 14:39:22 -0700, Mangabasi <[EMAIL PROTECTED]> wrote:
> Hi there,
>
> I need to translate the following code (rather something similar) to
> C++.  I have been studying C++ for the last two days but I could not
> find an easy way to do the following Python snippet.
...
> How do I make this work for
>
> C c2 = C(b, a)
>
> as well?
>
> Thank you in advance, I know this is somehow offtopic in the Python
> group but I would not dare asking this in the C++ groups.

In general, it is not a good idea to try to make a straight translation from
one language to another, especially between a very dynamic one like Python
to a very static one like C++ [1].  You might be able to do a straight
translation of some of the design, but soon you would find yourself working
against the language and missing out on its good features.

I think you'd be better off starting from scratch. (Or maybe not really from
scratch; having done a working version of the program once and maybe having
working tests is worth a lot.)

/Jorgen

[1] You might find C++ templates useful, though.

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python to C++ translation?

2005-07-18 Thread szymon . rozga
Ok so here's what you have:
A a(42);
B b(23.0);

a is now of type A, and b is now of type B. Right?

The constructor for class C takes in two arguments. The first one _has_
to be of type A and the second one _has_ to be of type B. This is how
you defined it.
This is why C(a,b) works fine. But C(b,a) shouldn't work because of how
you defined the constructor for C. You could add a second constructor
to C as follows:

C(B &_propB, A &_propA) {
propA = &_propA;
propB = &_propB;
}

which would let you call C(b,a).

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python to C++ translation?

2005-07-18 Thread George Sakkis
"Mangabasi" <[EMAIL PROTECTED]> wrote:

> Hi there,
>
> I need to translate the following code (rather something similar) to
> C++.  I have been studying C++ for the last two days but I could not
> find an easy way to do the following Python snippet.
>
>
> class A:
> def __init__(self, x):
> self.x = x
> def methodA():
> pass # Ignore the details
> class B:
> def __init__(self, x):
> self.x = x
> def methodB():
> def methodB():
> pass # Ignore the details
> class C:
> def __init__(self, A, B):
> self.A = A
> self.B = B
>
> a = A(5)
> b = B(5.5)
> c = C(a, b)
> print c.A.x
> print c.B.x
> #so far I can do it in C++
> #how do I do the following in C++?
> d = C(b, a)
> print d.A.x
> print d.B.x

In python this works thanks to "duck typing"; as long as both A and B instances 
have an attribute
'x', A.x and B.x work as expected without the need for type declarations. In 
C++ (and other
statically typed languages) there is no duck typing, so you can't translate it 
from python verbatim.
The typical (if not only) way to achieve the same in C++ is to have A and B 
inherit from a common
parent class, say X, and drag the common interface and implementation of A and 
B to X. Of course you
could (or perhaps should) do the same in python:

class X:
 def __init__(self, x):
 self.x = x

class A(X):
 def methodA():
 pass # Ignore the details

class B(X):
 def methodB():
pass # Ignore the details

class C:
 def __init__(self, x1, x2):
 self.x1 = x1
 self.x2 = x2

So the constructor of C would take two X instances, and the respective 
attributes x1 and x2 would be
declared as pointers to X.

HTH

George


-- 
http://mail.python.org/mailman/listinfo/python-list