Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Aliasing a class

47 views
Skip to first unread message

Mark Storkamp

unread,
Mar 9, 2017, 11:02:56 AM3/9/17
to
I'm somewhat familiar with c, but not so much with c++. I'm writing a
program for an Arduino micro-processor board, and the environment is c++.

For serial communication I generally use the Serial class. There are
calls such as: Serial.open(), Serial.print(), Serial.println(), etc.

Sometimes my code goes on to an Arduino ProMicro board, and then instead
of the Serial class I need to use the Serial1 class, e.g.
Serial1.open(), Serial1.print(), etc.

Fortunately the compiler does #define ARDUINO_AVR_PROMICRO when I'm
using the ProMicro board. So everywhere I do serial I/O I could do:

#ifdef ARDUINO_AVR_PROMICRO
Serial1.print(...); // or .open, or .println etc
#else
Serial.print(...);
#endif

But I would rather, in just one place, alias a name such as ComPort to
either Serial or Serial1, so the rest of my code could be:
ComPort.open(), ComPort.print(), etc.

Is there an easy way to do this?

Scott Lurndal

unread,
Mar 9, 2017, 11:50:27 AM3/9/17
to
Arrange for both Serial and Serial1 to inherit from a common
base class called ComPort, then set a pointer to the selected
serial class:

#if defined(ARDUINO_AVR_PROMICRO)
ComPort *port = &Serial1;
#else
ComPort *port = &Serial;
#endif

port->open(), port->print(), etc.

red floyd

unread,
Mar 9, 2017, 12:39:06 PM3/9/17
to
On 03/09/2017 08:02 AM, Mark Storkamp wrote:

There's always typedef. Or a using directive





#ifdef ARDUINO_AVR_PROMICRO
typedef Serial1 ComPort;
#else
typedef Serial ComPort;
#endif

Comport.print(...);


or

// C++11
#ifdef ARDUINO_AVR_PROMICRO
using ComPort = Serial1;
#else
using ComPort = Serial;
#endif

Comport.print(...);


Mark Storkamp

unread,
Mar 9, 2017, 1:25:57 PM3/9/17
to
In article <o9s3pl$amq$1...@dont-email.me>,
Thanks, that's exactly what I need. Don't know if it's C++11 or not, but
one way or the other should do it.

David Brown

unread,
Mar 9, 2017, 4:00:11 PM3/9/17
to
If you get /really/ stuck for this sort of thing, there is always #define:

#ifdef ARDUINO_AVR_PROMICRO
#define ComPort Serial1
#else
#define ComPort Serial
#endif

Comport.print(...);

Aim to use the typedef or using methods above - but if they don't suit
your needs here or in similar cases, #define is the "brute force" solution.

Mark Storkamp

unread,
Mar 10, 2017, 3:49:50 PM3/10/17
to
In article <mstorkamp-08467...@88-209-239-213.giganet.hu>,
No luck. Neither method worked. The error was "'Serial1' does not name a
type" for either approach. As a guess, it's because Serial1 is not the
type of the class, but an instantiation of some class that is set up for
me behind the scene.

A more simple:

#ifdef ARDUINO_AVR_PROMICRO
#define ComPort Serial1
#else
#define ComPort Serial
#endif

worked.

I never knew the pre-processor would do a substitution on what does not
look like a single token. I assumed it would see the class instance and
the method together as a single item.

Mark Storkamp

unread,
Mar 10, 2017, 3:50:29 PM3/10/17
to
In article <o9sfja$p16$2...@dont-email.me>,
The brute force method got the job done. Thanks.

Mark Storkamp

unread,
Mar 10, 2017, 3:57:48 PM3/10/17
to
In article <fJfwA.18268$VJ2....@fx40.iad>,
That might work, except for the first paragraph in my OP :-) But since
Serial and Serial1 seem to be instantiations of some unknown class, I
would have to dig pretty deep to find out what to inherit from, then add
the complication that all I know about c++ I learned in a class my
employer sent me to sometime around 1985.

Paavo Helde

unread,
Mar 10, 2017, 5:41:17 PM3/10/17
to
On 10.03.2017 22:50, Mark Storkamp wrote:
> In article <o9sfja$p16$2...@dont-email.me>,
> David Brown <david...@hesbynett.no> wrote:
>> If you get /really/ stuck for this sort of thing, there is always #define:
>>
>> #ifdef ARDUINO_AVR_PROMICRO
>> #define ComPort Serial1
> The brute force method got the job done. Thanks.

Do not thank, you will regret about this in the future!

Just my 2 cents of crystal ball...

peter koch

unread,
Mar 11, 2017, 7:24:25 AM3/11/17
to
Den fredag den 10. marts 2017 kl. 23.41.17 UTC+1 skrev Paavo Helde:
> On 10.03.2017 22:50, Mark Storkamp wrote:
> > In article <o9sfja$p16$2...@dont-email.me>,
> > David Brown <david...@hesbynett.no> wrote:
> >> If you get /really/ stuck for this sort of thing, there is always #define:
> >>
> >> #ifdef ARDUINO_AVR_PROMICRO
> >> #define ComPort Serial1
> > The brute force method got the job done. Thanks.
>
Agreed. Avoid those macros, they are going to bite back at you, leaving large wounds that heal slowly.

The error-message you gave indicates that Serial and Serial1 were not declared where you had your typedef. You can work around that by several means.
The cleanest solution is to include your serial header where you define your comport, e.g.

#ifdef ARDUINO_AVR_PROMICRO
#include <serial1.h>
using ComPort = Serial1;
#else
#include <serial.h>
using ComPort = Serial;
#endif

An alternative would be to explicitly declare the classes.
#ifdef ARDUINO_AVR_PROMICRO
class Serial1;
typename Serial1 ComPort;
#else

using ComPort = typename Serial;
#endif


Above you will see two ways of forward declaring stuff. The second one is the C++11 way.

Jorgen Grahn

unread,
Mar 11, 2017, 4:33:44 PM3/11/17
to
On Thu, 2017-03-09, Mark Storkamp wrote:
> I'm somewhat familiar with c, but not so much with c++. I'm writing a
> program for an Arduino micro-processor board, and the environment is c++.
>
> For serial communication I generally use the Serial class. There are
> calls such as: Serial.open(), Serial.print(), Serial.println(), etc.

That's a rather unusual and unfortunate choice. If the interface had
allowed you to create (or get a reference to) a Serial object, you
could have called the object or reference whatever you liked in the
rest of the code.

> Sometimes my code goes on to an Arduino ProMicro board, and then instead
> of the Serial class I need to use the Serial1 class, e.g.
> Serial1.open(), Serial1.print(), etc.
>
> Fortunately the compiler does #define ARDUINO_AVR_PROMICRO when I'm
> using the ProMicro board. So everywhere I do serial I/O I could do:
>
> #ifdef ARDUINO_AVR_PROMICRO
> Serial1.print(...); // or .open, or .println etc
> #else
> Serial.print(...);
> #endif
>
> But I would rather, in just one place, alias a name such as ComPort to
> either Serial or Serial1, so the rest of my code could be:
> ComPort.open(), ComPort.print(), etc.
>
> Is there an easy way to do this?

If an alias is what you want, it seems to me both C and C++ gives you
easy ways to get it:

typedef Serial1 ComPort; // C, C++
using ComPort = Serial1; // C++11

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Alf P. Steinbach

unread,
Mar 11, 2017, 5:33:04 PM3/11/17
to
Sorry, I sent this as mail by mistake.

On 09-Mar-17 5:02 PM, Mark Storkamp wrote:
> I'm somewhat familiar with c, but not so much with c++. I'm writing a
> program for an Arduino micro-processor board, and the environment is c++.
>
> For serial communication I generally use the Serial class. There are
> calls such as: Serial.open(), Serial.print(), Serial.println(), etc.

It that's literally the syntax you're using, then `Serial` is a variable.


> Sometimes my code goes on to an Arduino ProMicro board, and then instead
> of the Serial class I need to use the Serial1 class, e.g.
> Serial1.open(), Serial1.print(), etc.

Ditto.

It that's literally the syntax you're using, then `Serial1` is a variable.


> Fortunately the compiler does #define ARDUINO_AVR_PROMICRO when I'm
> using the ProMicro board. So everywhere I do serial I/O I could do:
>
> #ifdef ARDUINO_AVR_PROMICRO
> Serial1.print(...); // or .open, or .println etc
> #else
> Serial.print(...);
> #endif
>
> But I would rather, in just one place, alias a name such as ComPort to
> either Serial or Serial1, so the rest of my code could be:
> ComPort.open(), ComPort.print(), etc.
>
> Is there an easy way to do this?

Assuming those two apparent variables really are variables, then


#ifdef ARDUINO_AVR_PROMICRO
auto& my_serial = Serial1;
#else
auto& my_serial = Serial;
#endif


Cheers & hth.,

- Alf

Juha Nieminen

unread,
Mar 13, 2017, 3:20:24 AM3/13/17
to
red floyd <no....@its.invalid> wrote:
> There's always typedef. Or a using directive

How exactly does "Serial.println()" indicate that "Serial" is a type?

If "Serial" and "Serial1" are objects, then what you want is a reference.
0 new messages