0

I have many classes which are like this:

struct A_Insert
{
    Data &d;
    A_Insert(d) : d(d){}

    // ...
}

struct A_Query
{
    Data &d;
    A_Query(d) : d(d){}

    // ...
}

struct B_Insert
{
    Data &d;
    B_Insert(d) : d(d){}

    // ...
}

struct B_Query
{
    Data &d;
    B_Query(d) : d(d){}

    // ...
}

The best thing I can think to do is define a macro like this:

#define Data_Query(name, body) struct name{Data &d; name(Data &d) : d(d) {} body}

but this leads to somewhat ugly code as I have to use parentheses to define my structs and my IDE doesn't handle it very well.

Data_Query(A_Insert, 
    int bind_params(stmt &stmt){}
    ...
)

I would like a macro which could allow me to get the name of the containing type for the constructor so I could write code like:

#define constructor CONTAINING_TYPE
#define Data_Construct Data &d; constructor (Data &d) : d(d) {}

struct A_Insert { Data_Construct // ... }

and this way my IDE could treat it like a normal struct declaration. Any suggestions would be appreciated.

12
  • 1
    it is the nature of macros that they lead to somewhat ugly code. It is not clear why you want to use a macro in the first place. Commented Jul 27, 2020 at 11:51
  • @idclev463035818 it can sometimes be tedious to type out the name of the class for each new constructor. I would like if there was something like the javascript constructor keyword which define a constructor while making it easy to copy and paste and keeping the code very readable. I will add that to my question. Commented Jul 27, 2020 at 11:52
  • I don't see any reason for this. If you are using a halfway decent IDE it sure supports templates and auto complete. Commented Jul 27, 2020 at 11:57
  • 2
    @bipll true words, but I dare to claim that code with macros tends to be more ugly than code without Commented Jul 27, 2020 at 12:00
  • 2
    No, it is not possible in C++. Of course almost everything is possible with macros, but where I work, it is only possible once. Commented Jul 27, 2020 at 12:03

1 Answer 1

2

Inheritance:

struct DataReference
{
    Data &d;
    
    DataReference(Data& d) : d(d) {}
};

struct A_Insert : DataReference
{
    using DataReference::DataReference;

    ...
};
Sign up to request clarification or add additional context in comments.

5 Comments

What is the purpose of using DataReference::DataReference;? This seems to pull the DataReference constructor into lookups within A_Insert, but it's already found there anyway. I don't see any purpose to it, am I missing something?
I don't know. But the compiler I use right now has a c++ version that doesn't automatically do that.
@ArthurTacca I tried it without that and it didn't work. I don't really understand it but it seems to work.
Ah I see: I thought that in the ... you were intending to declare another constructor that made use of DataReference's constructor. But actually you're suggesting that you don't write a constructor at all on the derived class at all. Instead, the using turns the DataReference(Data& d) into a A_Insert(Data& d) constructor automatically.
This is the closest thing to what I wanted of having a constructor without having to write the declaring type because the 'using' doesn't require the name of the containing type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.