delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/07/19:57:59

From: Laurence Withers <lwithers AT lwithers DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Annoying: const char * in Allegro
Date: Tue, 7 Sep 1999 14:20:42 +0100
Message-ID: <NrGOGVAqER13EAvt@lwithers.demon.co.uk>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 990906190544 DOT 12553A-100000 AT is>
<37d4ce7c DOT 0 AT news DOT uni-bielefeld DOT de>
NNTP-Posting-Host: lwithers.demon.co.uk
X-NNTP-Posting-Host: lwithers.demon.co.uk:194.222.80.1
X-Trace: news.demon.co.uk 936740168 nnrp-01:28960 NO-IDENT lwithers.demon.co.uk:194.222.80.1
X-Complaints-To: abuse AT demon DOT net
MIME-Version: 1.0
X-Newsreader: Turnpike (32) Version 4.01 <WCcCG$mmNL32sYSf46vBeC80CR>
Lines: 74
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <37d4ce7c DOT 0 AT news DOT uni-bielefeld DOT de>, Manni Heumann
<manfred DOT heumann AT uni-bielefeld DOT de> writes

[snip - no 'const' in Allegro]
>But the problem is there anyway. It's either the developer or the user of the 
>library who has to deal with it. And since 2.95 it got serious, because 
>sources that pass something supposed to be const to a non-const function will 
>no longer compile.

- cut here -------------------------------------------------------------

#include <iostream>
#include <cstring>

void test0(const char const*&) throw();
void test1(char*&) throw();

int main()
{
    char* Q = new char[30];
    strcpy(Q, "Hello, C++ world!");
    cout << "Before test0: " << Q << endl;
    test0(Q);
    cout << "After test0: " << Q << endl;
    delete [] Q;
}

void test0(const char const*& Q) throw()
{
    test1(Q);
}

void test1(char*& Q) throw()
{
    delete [] Q;
    Q = new char[30];
    strcpy(Q, "Hello again, C++ world!");
}

- cut here -------------------------------------------------------------

> gxx -fpermissive -o const.exe const.cpp
> const
Before test0: Hello, C++ world!
After test0: Hello again, C++ world!

>So if you are programming in C++ and use strings you will have to resort to 
>some ugly hacks in your code.

The code itself is unaffected if you use -fpermissive. Of course, it
will compile with warnings, and does enable you to do silly things:

  string s;
  do_uconvert("don't try this at home", U_ASCII, s.c_str(), U_UTF8, 99);

This will overwrite memory that does not belong to s.

>It seems the problem really is in the varargs macros and Shawn is off the 
>hook, so the question seems to be what the C++ guys want to do about all this.
>Suggestions, anybody?

If you are using C++ string objects, you don't really need to use
Allegro's varargs stuff, because you can use strstream objects, etc.
Although I admit allegro_message() might be fundamental to a program.

If you are willing to wait, I'm trying to write a set of C++ wrappers
for Allegro which will, at least, solve the lack of const problems.
Whether making the Allegro sources const-correct would be worthwhile, or
even possible, I'll leave to somebody more experienced to decide.

HTH, and bye for now,
-- 
Laurence Withers, lwithers AT lwithers DOT demon DOT co DOT uk
                http://www.lwithers.demon.co.uk/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019