[odb-users] inheritance and derived class transient members

MM finjulhich at gmail.com
Wed Aug 19 15:26:31 EDT 2015


Hi, let me put here the compilable code and ask the question below:

template <typename base, typename derived>
class cloneable : public base
{
protected:
  using base::base;

  virtual base* clone() const override
  {
    return new derived{static_cast<derived const &>(*this)};
  }
};

struct Base {
  std::string        code;
  std::string        desc;
  const std::string* F1_;
  const std::string* F2_;
  std::uint32_t      id;

  virtual const std::string& type() const =0;
  virtual Base* clone() const =0;
  virtual ~Base() =0;
};

void set_F1(Base&, const std::string&);
void set_F2(Base&, const std::string&);

struct Derived : public cloneable<Base,Derived> {
  std::string F3;
  static const std::string mtype;
private:
  const std::string& type() const override;
};


// 1 Base_odb.hpp : from which the odb compiler
generates Base-odb.hxx, Base-odb.ixx, Base-odb.cxx
#pragma db object(Base) abstract definition
#pragma db member(Base::F1_) transient
#pragma db member(Base::F1) virtual(std::string)\
  get(this.set_F1? *this.set_F1_: "") set(set_F1 (this, (?)))
#pragma db member(Base::F2_) transient
#pragma db member(Base::F2) virtual(std::string)\
  get(this.F2_? *this.F2_: "") set(set_F2 (this, (?)))


//2 Derived_odb.hpp =>
odb: Derived-odb.hxx, Derived-odb.ixx, Derived-odb.cxx
#include "Base_odb.hpp"
#pragma db object(Derived) table("Tbld") definition
#pragma db member(Derived::id_) virtual(std::uint32_t) access(Base::id) id
auto


Querying the db with        db.query<Derived>();
creates Derived objects where F3 is populated correctly, but the base
struct members: code, desc, F1_, F2_ are not populated. But id is populated.

I added:

#pragma db member(Derived::code) access(Base::code)

It compiled, but still failed to populate 'code' in the object.


I would have thought the base class shoudl get populated too.

I can see indeed the SQL query doesn't SELECT any of the base class columns
in the Derived table.

Thansk,

MM


More information about the odb-users mailing list