Advertisement

C++ additions

Started by August 10, 2000 07:37 AM
16 comments, last by Kylotan 24 years, 4 months ago
There was a recent thread about what we thought that C++ lacks... which I missed through being away. I'd like to add a couple of suggestions: 1) A complete rewrite. Face it, the language is a mess I accept that many of the features are to maintain backwards compatibility with C, without which, probably no-one would use the language. But I am talking ideals here. Examples... The 'this' pointer shouldn't be a pointer, it should be a const reference, but sadly it was added to the language before references were. The 'string' class should probably be part of the language, not something you have to #include. Templates are a mess: their purpose and usefulness is amazing, and gives you logical flexibility beyond that offered by Java etc, but the implementation is hideous, barely more than a glorified preprocessor, increasing compile times often by an order of magnitude... having to show private members in header files is an annoyance to those who wish to distribute libraries with headers without divulging their intellectual property details... is there no way of separating it out so that interface and implementation can be in different places, or alter the compiling/linking process to facilitate this as in Java? 2) Ok, a serious suggestion now: the 'with' keyword. Some of you will be familiar with 'with' (heh) from experiences using every other modern language, but for those who aren't, here's a quick example:
    
class Object
{
    int property1;
    string property2;
};

Object list[10];

// Iterate through the list and set up each object
for (int i=0; i < 10; ++i)
{
    with (list)
    {
        property1 = rand() * 10;
        property2 = "nothing yet";
    }
}
  </pre>  

It simply acts as a shorthand to save you having to access an object explicitly. Just as being in a member function block will allow implicit consideration of the 'this' pointer when resolving identifier names, being within a 'with' block will consider the object specified in the with block's parameter.

3) Properties: see how Visual C++ and C++ Builder implement these: essentially allowing you to map a Get() and Set() function to a member variable, which is called appropriately depending on whether that variable is used as an RValue or an LValue… it simply cuts down on calling code… perhaps it could be claimed that it obscures the purpose, but then again, so can operator overloading. It does allow for full encapsulation of variables without it -looking- like it is encapsulated… a win for the interface designers, I believe.

It is a feature worth -considering- if nothing else.

Example:
<pre>
class Object
{
public:
    property(get=GetValue, put=SetValue) int value;
private:
    void SetValue(int x) { if (value > 0) value = x; else value = 0 }
    int GetValue() { return value;}
};

Object test;

test.value = 50; // value is set to 50
cout << test.value; // shows 50
test.value = -45; // illegal value, SetValue stores 0
cout << test.value; // shows 0
      </pre>  

Another example would be to able to do this:

property(get=GetValue, put=void/null/const) int value;

(The standard people would choose the most appropriate keyword: I don't particularly care which.)

The effect would be to allow read access to that variable through the public interface, but not write access. It would throw up a 'value is not an LValue' error, just like writing to a const variable, except by private members of the class it belongs to. This would clean up all those classes we must have done where there are lots of Get() functions, but no Set() functions (eg. the constructor is responsible for all such changes). The implementation would need more thought than I have given it though due to the potential ambiguities between modifying it from inside the class (not via the Set function) and outside the class (via the Set function). If that could be resolved though, it could be useful.

Comments?


Edited by - Kylotan on 8/10/00 7:44:57 AM    
That''s a nice speech.

Although I disagree and say that C++ is fantastic, I would find it just as easy to code in plain old C.

Things like templates, derived classes etc. are hardly ever used by game programmers (I currently use inheritance for my renderers in my engine).

The thing that you''re missing is that C++ is NOT JUST USED TO MAKE GAMES! Think of the way that Excel is made - features like templates, inheritance, constructors & destructors are so inconceivably useful that you couldn''t have the program without it.

Personally, I don''t have any problem using these features, it all depends on your coding style. If you use things like Hungarian Notation (ah, the old favourite) and proper tab indentation then your code will be easy to understand, even if it does use the most complicated of C++ techniques.

On reflection, I couldn''t see a better way to organize the C++ features anyway!



========
Smidge
www.smidge-tech.co.uk
========
--Mr Smidge
Advertisement
quote:
It is a feature worth -considering- if nothing else.

Example:


class Object{public: property(get=GetValue, put=SetValue) int value;private: void SetValue(int x) { if (value > 0) value = x; else value = 0 } int GetValue() { return value;}};Object test;test.value = 50; // value is set to 50cout << test.value; // shows 50test.value = -45; // illegal value, SetValue stores 0cout << test.value; // shows 0

Another example would be to able to do this:

property(get=GetValue, put=void/null/const) int value;


This feature already exists, so there''s no need for it...

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
quote: Original post by smidge_tech

Things like templates, derived classes etc. are hardly ever used by game programmers (I currently use inheritance for my renderers in my engine).


Only cos most game programmers are behind the times My games use lots of both, especially all the templates in the STL.

quote:
The thing that you''re missing is that C++ is NOT JUST USED TO MAKE GAMES! Think of the way that Excel is made - features like templates, inheritance, constructors & destructors are so inconceivably useful that you couldn''t have the program without it.


Umm, it would appear that you have mistaken me for someone who doesn''t like these features I find them useful in game programming, too. But if you sat down with these features to make a new language, you could do it in a cleaner and better way. Just a shame that there would be an installed user base of zero.

Here''s another idea: you could do away with global variables, and instead have 1 global class called Main. You could therefore order and specify the initialisation of your global variables through Main() constructor initialiser list, Main.run() would be called at the start rather than int main(), etc.

quote:
Personally, I don''t have any problem using these features, it all depends on your coding style. If you use things like Hungarian Notation (ah, the old favourite) and proper tab indentation then your code will be easy to understand, even if it does use the most complicated of C++ techniques.


I can use all these features just fine... which is how I am able to see ways in which they could have been better implemented Very few of the ''defects'' are because the C++ authors lacked foresight, they are more down to deriving it from C, which is a very old language and not totally in tune with all the new features.
quote: Original post by Gladiator

This feature already exists, so there''s no need for it...


It is not a part of C++, it is an extension implemented in Microsoft C++ and Borland C++, both implemented slightly differently I believe. I am talking about additions to the language itself, rather than individual compiler vendor''s implementations of it.



I would vote against including string in the language because that would clutter up the global namespace. Am I not right? I actually like how it''s set up already, honestly.

And (being honest again ) I do not mind typing in the additional . or -> when accessing members of an object.

As far as templates go, I heartily agree, but I don''t have a solution either I was researching this on g++ and they had made some #pragmas that let you declare which file was the declaration and which file was the implementation. But then I became (and still am) confused when I read that those #pragmas had become obsolete and I couldn''t find the newer and better way to do it.
joeG
Advertisement
well, what more could you want... OO Pascal itself doesnt have the get/set property thing either, but Delphi does... which is the C++ case...

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
Klyotan:

If you use the STL you can in fact get something very similar to your 'with' statement. In some ways, better in fact.

For example:

        struct myThingToDoOnEachElement : public std::unary_function<void, MyType>{void operator()(MyType &mt)    {    // do something with mt    }}std::list<MyType> theList;// build elements in the list// here is the equivalent to your 'with' statementstd::for_each( theList.begin(), theList.end(), myThingToDoOnEachElement() );        


This is actually known to be quite efficient, you can inspect your STL implementation to confirm this.

Ironically, you do with this templates.

I strongly disagree with your opinion on templates. They are useful everywhere, regardless of the domain, whether it be Excel or a game.

Who wants to write yet another linked list?

And templates are way way beyond a glorified pre-processor.
There is supposed to be a way to use an external template definition, but I don't think most compilers support it. And certainly not VC++, which is notoriously bad with its support of the C++ standard.

I can't imagine C++ being as powerful without templates. They definately help reduce the amount of non-domain specific code you need to write.

Anyway, I'll get off my soapbox now.

Edited by - SteveC on August 10, 2000 12:35:36 PM
quote: Original post by joeG

I would vote against including string in the language because that would clutter up the global namespace. Am I not right? I actually like how it''s set up already, honestly.


Sure, and why don''t we have to #include < integer.h > for int and < char.h > for char, too?

There''s plenty of room in the namespace. Other languages (ok, pretty much -every- other language) cope ok with a String in there somewhere.

A string is an essential type that you can barely do without. The ''array of characters'' implementation is also the main stumbling block that most new C++ programmers have trouble dealing with. You can see from the posts on this board, that the idea of not being able to use ''='' to copy them, ''=='' to compare them, problems of overwriting the bounds, having to allocate memory, etc etc... all burden the new guy. I wonder how many people gave up on C++ because of its poor support for strings?

quote:
And (being honest again ) I do not mind typing in the additional . or -> when accessing members of an object.


Well, some people don''t mind typing in assembly either... there is always a way around a feature if you like, I was just suggesting a few things which can aid productivity. Personally, this kind of thing gets boring to me:

character->playerdata->stats->personalstats->iq = 100;
character->playerdata->stats->personalstats->dex = 80;
character->playerdata->stats->personalstats->str = 110;
...
Of course, you can use a reference to cut all this down, but why create intermediate objects, when the compiler could be resolving all this for you...
quote: Original post by Gladiator

well, what more could you want... OO Pascal itself doesnt have the get/set property thing either, but Delphi does... which is the C++ case...


I am really not sure what you''re getting at... Delphi is a single implementation of a language and it has it set up to facilitate the visual interface with the objects. However, I am talking about adding it to the C++ language as a whole so everyone can use it, not just people who use Visual C++ and Borland C++ Builder, and to make the keyword standard across all platforms.

This topic is closed to new replies.

Advertisement