Inheritance

O

osmium

deepu said:
Please let me know how to concat two string using multiple in
heritance

My reaction to that is that your instructor is in over his head in teaching
C++.
 
J

James Kanze

The question is not clear. Concatenating strings is
ordinarily a run-time operation, whereas multiple inheritance
is ordinarily a compile-time operation. There is, by
definition, no way to perform a run-time operation at
compile-time, nor vice versa.

Yes and no. There are three ways to concatenate strings in C++,
depending on what you mean by "string". If the string
represents a token in the source code, you can use ## in a
macro---formally, this concatenates tokens, not strings, but in
most people's minds, there isn't much different. If he's
talking about concatenating string literals (the only real
compile-time "strings"), then just juxtaposing them is
sufficient. And of course, std::string overrides the + operator
to concatenate a runtime (and this is, of course, usually what
is meant by concatenation of strings).

The first two possibilities are compile time (but they still
have absolutely nothing to do with inheritance).
 
P

Pascal J. Bourguignon

deepu said:
Please let me know how to concat two string using multiple in
heritance

Well, if you ask something ludicruous, don't be surprised:


#include <string>
#include <iostream>

class FirstString : public std::string{
public:
FirstString(std::string str):std::string(str){};
std::string str(){return *this;}
};
class SecondString : public std::string{
public:
SecondString(std::string str):std::string(str){};
std::string str(){return *this;}
};
class ConcatenatedString : public FirstString,public SecondString{
public:
ConcatenatedString(std::string first,std::string second):FirstString(first),SecondString(second){}
std::string str(){ return FirstString::str()+SecondString::str(); }
};

int main(){
ConcatenatedString c("hello ","world!");
std::cout<<c.str()<<std::endl;
return(0);
}

/*
-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Thu Jun 4 10:35:49

SRC="/tmp/s.c++" ; EXE="s" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
hello world!
status = 0

Compilation finished at Thu Jun 4 10:35:49
*/
 
J

James Kanze

[...]
I believe you are mistaken there. I cannot answer for "most
people's minds," but I certainly expect most C++ developers to
understand the difference between a string and a token.

One would hope so. Where the ambiguity lies is in the behavior
of the ## operator. In a very real sense, "concatenating a
token" is a non-sense (even if the C and the C++ standard talk
about it). What you are actually concatenating is the original
string representation of the token, e.g.
#define CONCAT2(a,b) a ## b
#define CONCAT(a,b) CONCAT2(a,b)
int CONCAT(name_,0x0A) ;
is required to expand into:
int name_0x0A ;
and not
int name_10 ;
regardless of the internal representation of integer literals,
the actual value, and the fact that 10 and 0x0A are exactly the
same "token" (or more clearly stated, that the strings "10" and
"0x0A" tokenize to exactly the same thing).

I think most programmers think of ## as concatenating "strings",
or at least, the "string representation". I know many
programmers who have been surprised by g++'s error messages
when the results of the concatenation isn't a legal token (which
is undefined behavior according to the standard, but works in
every other compiler I've ever seen). I'll admit that while g++
is applying (or trying to apply) the letter of the law, it
doesn't make sense to me: what is or isn't a token is context
dependent (the "..." after #include *isn't* a string literal,
for example, and if you take the standard literally, cannot be
created by the # operator---although even g++ accepts this).
If you're trying to talk in terms of concatenating tokens, you
have to first define what it means to concatenate a token. Why
is something like "CONCAT(x,5e0)" legal, but "CONCAT(x,5.0)"
not, when they both involve exactly the same tokens?
The popular debate seems to be whether raw arrays of char
should be considered strings.
That is true, insofar as we consider character array literals
to be strings. In this sense, I agree that most developers do
consider "hello world" a string, even in C++.

According to the standard, it is a "null-terminated multibyte
string literal", which is a bit verbose for everyday use:).
It's a string, even if it isn't what I normally think of under
the term "string", when the word is used without more context
when speaking about C++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top