phimuemue
phimuemue

Reputation: 35993

Is there kind of "attribute" keyword in C++?

maybe I missed something, but I'm wondering about the following:

At the Mozilla Developer Pages about Coding Guidelines, I read the following:

Whenever you are retrieving or setting a single value without any context, you should use attributes. Don't use two methods when you could use one attribute. Using attributes logically connects the getting and setting of a value, and makes scripted code look cleaner.

This example has too many methods:

 interface nsIFoo : nsISupports {
     long getLength();
     void setLength(in long length);
     long getColor(); 
 };

The code below will generate the exact same C++ signature, but is more script-friendly.

interface nsIFoo : nsISupports {
    attribute long length;
    readonly attribute long color; 
};

What I'm thinking about is the attribute long length. I assume that this syntax aucomatically creates getter/setter methods.

Upvotes: 6

Views: 617

Answers (3)

templatetypedef
templatetypedef

Reputation: 372814

Mozilla uses a language called IDL (interface definition language) to define interfaces for objects that are used in multilanguage contexts, such as both C++ and JavaScript. It compiles down into code in these two languages and therefore allows developers working on the project to have a single definition for their interfaces in as many languages as they'd like. So no, this isn't standard C++ code; it's something entirely different.

On a related note, interface and readonly aren't C++ keywords either. :-)

Upvotes: 12

CB Bailey
CB Bailey

Reputation: 792119

This coding guideline applies to IDL, not C++, so no, attribute is not part of C++. The guidelines is taking about the C++ signatures that the IDL generates.

Upvotes: 5

Karl von Moor
Karl von Moor

Reputation: 8614

This

interface nsIFoo : nsISupports {
    attribute long length;
    readonly attribute long color; 
};

is Interface Description Language (Used for C++ – Javascript interop). Mozilla has a tool which generates C++ code for this, with usual getter and setter methods.

C++ itself doesn't have an attribute keyword.

Upvotes: 3

Related Questions