public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* find_overload_match for constructor
@ 2015-05-08  9:29 Weller
  2015-05-29 11:29 ` Weller
  0 siblings, 1 reply; 4+ messages in thread
From: Weller @ 2015-05-08  9:29 UTC (permalink / raw)
  To: gdb

Hey everyone,

I'm currently stomped on how to call_function_by_hand for a constructor.
Basically I have a class in C++ which wraps multiple native data types to
the underlying base class like this:

class constant_t: public value_t
{ constant_t (int);
  constant_t (double);
  constant_t (const char *);
}

Now when I have a command line like
(gdb) p $testnr1 + 0.1
I need the function call to the constructor of constant_t so that I can
use the operator+ on the value_t of $testnr1.

I looked through the code and the only thing I could find which came
close to this was the code used for the operator overload on C++ classes
with find_overload_match but I can't quite figure out how I would call
this in case of an overloaded constructor. Do I need to create an object
of type constant_t with it being passed to **args and **objp? In that
case how would I achieve that, as the the created object is kinda what I
want in the first place.

Any help is appreciated,
Lennart

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: find_overload_match for constructor
  2015-05-08  9:29 find_overload_match for constructor Weller
@ 2015-05-29 11:29 ` Weller
  2015-05-29 11:35   ` Jan Kratochvil
  0 siblings, 1 reply; 4+ messages in thread
From: Weller @ 2015-05-29 11:29 UTC (permalink / raw)
  To: gdb

[-- Attachment #1: Type: text/plain, Size: 1516 bytes --]

Still working on this problem. A full example would lool like the
attachment. A being the base class and B being the constant class.

Another option would be to do the calls manually. e.g.
A a1 = A (INT)
a1->i = 123

But it still presents the same issue. I need to find the correct
constructor which takes the type argument so its not really an
improvement.
Anyone got any ideas how this could be achieved?

Cheers,
Lennart Weller

On Fri, May 08, 2015 at 11:29:06AM +0200, Weller wrote:
> Hey everyone,
> 
> I'm currently stomped on how to call_function_by_hand for a constructor.
> Basically I have a class in C++ which wraps multiple native data types to
> the underlying base class like this:
> 
> class constant_t: public value_t
> { constant_t (int);
>   constant_t (double);
>   constant_t (const char *);
> }
> 
> Now when I have a command line like
> (gdb) p $testnr1 + 0.1
> I need the function call to the constructor of constant_t so that I can
> use the operator+ on the value_t of $testnr1.
> 
> I looked through the code and the only thing I could find which came
> close to this was the code used for the operator overload on C++ classes
> with find_overload_match but I can't quite figure out how I would call
> this in case of an overloaded constructor. Do I need to create an object
> of type constant_t with it being passed to **args and **objp? In that
> case how would I achieve that, as the the created object is kinda what I
> want in the first place.
> 
> Any help is appreciated,
> Lennart

[-- Attachment #2: simple.cpp --]
[-- Type: text/x-c++src, Size: 490 bytes --]

#include <string.h>

enum T {
  INT,
  DOUBLE,
  STRING
};

class A {
public:
  T t;
  int i;
  double d;
  char *s;

  A (enum T);
};

class B : public A {
public:
  B (int);
  B (double);
  B (const char *);
};

A::A (enum T t) {
  this->t = t;
}

B::B (int i) : A (INT) {
  this->i = i;
}

B::B (double d) : A (DOUBLE) {
  this->d = d;
}

B::B (const char *s) : A (STRING) {
  this->s = strdup (s);
}

int main () {
  A a1 = B (123);
  A a2 = B (1.23);
  A a3 = B ("foo");
  return 0;
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: find_overload_match for constructor
  2015-05-29 11:29 ` Weller
@ 2015-05-29 11:35   ` Jan Kratochvil
  2015-05-29 11:42     ` Weller
  0 siblings, 1 reply; 4+ messages in thread
From: Jan Kratochvil @ 2015-05-29 11:35 UTC (permalink / raw)
  To: Weller; +Cc: gdb, Phil Muldoon

On Fri, 29 May 2015 13:29:31 +0200, Weller wrote:
> Anyone got any ideas how this could be achieved?

Officially it is not supported.

I remember I achieved that one by calling malloc() and passing the address as
the first parameter to a constructor (its 'this') but that is a hack.

It should be supported by the 'compile code' integration with GCC but its C++
part is not yet ready.


Jan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: find_overload_match for constructor
  2015-05-29 11:35   ` Jan Kratochvil
@ 2015-05-29 11:42     ` Weller
  0 siblings, 0 replies; 4+ messages in thread
From: Weller @ 2015-05-29 11:42 UTC (permalink / raw)
  To: Jan Kratochvil; +Cc: gdb

Okay thanks for the quick answer. I might try that hack and see if it
works good enough for my problem. If not I'll have to include a few
extra functions in the library to allow non-constructor calls for the
class.

Lennart

On Fri, May 29, 2015 at 01:35:24PM +0200, Jan Kratochvil wrote:
> On Fri, 29 May 2015 13:29:31 +0200, Weller wrote:
> > Anyone got any ideas how this could be achieved?
> 
> Officially it is not supported.
> 
> I remember I achieved that one by calling malloc() and passing the address as
> the first parameter to a constructor (its 'this') but that is a hack.
> 
> It should be supported by the 'compile code' integration with GCC but its C++
> part is not yet ready.
> 
> 
> Jan

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-05-29 11:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-08  9:29 find_overload_match for constructor Weller
2015-05-29 11:29 ` Weller
2015-05-29 11:35   ` Jan Kratochvil
2015-05-29 11:42     ` Weller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).