如何访问STL字符串类中的成员变量? 我正在研究C++中的早期对象第七版的书中的一个编程挑战,其中一个作业要求创建一个从STL字符串类派生的类。我发布这个问题的目的是了解我可以做什么,以及我应该如何实施解决方案,这样就没有人提供更高级的建议了

如何访问STL字符串类中的成员变量? 我正在研究C++中的早期对象第七版的书中的一个编程挑战,其中一个作业要求创建一个从STL字符串类派生的类。我发布这个问题的目的是了解我可以做什么,以及我应该如何实施解决方案,这样就没有人提供更高级的建议了,c++,string,class,C++,String,Class,--如文中所述的问题-- 回文测试 回文是一个前后读相同的字符串。例如,单词mom、dad、madam和radar是回文。编写一个从STL字符串类派生的class Pstring。Pstring类添加了一个成员函数 bool isPalindrome() 它确定字符串是否为回文。包含一个构造函数,该构造函数将STL string对象作为参数,并将其传递给字符串基类构造函数。通过让主程序要求用户输入字符串来测试类。程序使用该字符串初始化Pstring对象,然后调用isAlindrome()确定输

--如文中所述的问题--

回文测试

回文是一个前后读相同的字符串。例如,单词mom、dad、madam和radar是回文。编写一个从
STL字符串类派生的
class Pstring
Pstring类
添加了一个成员函数

bool isPalindrome()
它确定字符串是否为回文。包含一个构造函数,该构造函数将
STL string
对象作为参数,并将其传递给字符串基类构造函数。通过让主程序要求用户输入字符串来测试类。程序使用该字符串初始化Pstring对象,然后调用isAlindrome()确定输入的字符串是否为回文

您可能会发现使用string类的下标运算符[]很有用:如果str是string对象,k是整数,则str[k]返回字符串中位置k处的字符

--结束--

我的主要问题是,如果我从中派生Pstring的类不是我编写的类,并且我不知道它如何实现其成员,那么如何访问保存字符串对象的成员变量

比如说,

#include <string>
using namespace std;

class Pstring : public string
{
public:
  Pstring(std::string text)
   : string(text) { }

  bool isPalindrome()
  {
    // How do I access the string if I am passing it to the base class?

    // What I think I should do is...
    bool is_palindrome = true;
    auto iBegin = begin();
    auto iEnd   = end() - 1;

    while (iBegin < iEnd && is_palindrome)
    {
      if (*iBegin++ != *iEnd--)
        is_palindrome = false;
    }

    return is_palindrome;

    // But I think this is wrong because...
    // #1 The book did not discuss the keyword auto yet
    // #2 The book discussed when a class is derived from another class,
    //    how the members from super class will be accessible to the sub class.
    //    However, with this assignment, I don't see how to access the members.
  }
}
#包括
使用名称空间std;
类Pstring:公共字符串
{
公众:
Pstring(标准::字符串文本)
:字符串(文本){}
bool isPalindrome()
{
//如果将字符串传递给基类,如何访问该字符串?
//我想我应该做的是。。。
bool is_palindrome=true;
自动iBegin=begin();
自动iEnd=end()-1;
while(iBegin
我觉得这样做不正确的原因是因为赋值提到使用下标符号,但是,如果我不知道存储字符串的变量的名称,我不理解如何使用下标符号


任何帮助都将不胜感激,因为作者不提供解决方案,除非我是一名在我看来相当蹩脚的讲师。这可能与这是一个学术文本这一事实有关。

您不应该继承std::string,因为它不是为此而设计的,您也不需要继承它来查找回文

见此:

回文解决方案(来自此问题:链接自此:)


如果您不想使用auto,那么您可以简单地使用
std::string::iterator
,这就是在本例中
auto
要解决的问题

因此,问题1得到了满足


调用
begin()
end()
时,调用的是超类std::string中的成员
begin()
end()


因此,问题2得到了解决。

这本书没有涵盖
auto
,因为该关键字最近才添加到该语言中。如果你的编译器已经一年多了,或者不是一个大的名字,它可能不支持它

对于这个问题,您不需要访问任何成员变量来获得适当的解决方案,因此无需担心它们是什么或它们是否可访问。好事情,因为这些都不是标准规定的——都是特定编译器定义的实现细节,如果你发现自己在深入挖掘,你应该问问自己你做错了什么

当然,父类的成员函数完全可以像子类的成员函数一样进行访问——您只需调用它们

成员运算符重载有点棘手,但仍然不太糟糕。您需要提供调用它们的实例,即
*this
。您也可以使用
操作符
关键字调用它们,但在我看来这有点笨拙

if ((*this)[i] == (*this)[j])

if (operator[](i) == operator[](j))
试试这个:

#include <string>

class Pstring : public std::string
{
public:
    Pstring(const std::string &text)
        : std::string(text) { }

    bool isPalindrome()
    {
        std::string::size_type len = length();
        std::string::size_type half = len / 2;
        for (std::string::size_type idx = 0; idx < half; ++idx)
        {
            if ((*this)[idx] != (*this)[len-idx-1])
                return false;
        }
        return true;
    }
};
#包括
类Pstring:public std::string
{
公众:
Pstring(常量标准::字符串和文本)
:std::字符串(文本){}
bool isPalindrome()
{
std::string::size_type len=length();
std::string::size_type half=len/2;
对于(std::string::size_type idx=0;idx
永远不要从stl类派生。总是个坏主意。你为什么不试试组合呢?作者可能不会提供解决方案,因为他是个白痴。@Mehrdad:STL容器和字符串类不是用来派生的。它们不提供虚拟析构函数来让派生类清理其资源,它们不提供对任何成员的
受保护的
访问。还要注意的是,
private
继承与
public
继承截然不同<代码>私有< /COD>继承更像“按术语实现”,而<>代码>公共< /COD>继承是“是”。“我正在研究一本书中的编程挑战,该书从C++早期对象第七版开始,其中一个作业要求创建一个由STL字符串类派生的类。”自我提醒:除了我们,永远不要买这本书,阅读它,或者用它做任何事情
if ((*this)[i] == (*this)[j])

if (operator[](i) == operator[](j))
#include <string>

class Pstring : public std::string
{
public:
    Pstring(const std::string &text)
        : std::string(text) { }

    bool isPalindrome()
    {
        std::string::size_type len = length();
        std::string::size_type half = len / 2;
        for (std::string::size_type idx = 0; idx < half; ++idx)
        {
            if ((*this)[idx] != (*this)[len-idx-1])
                return false;
        }
        return true;
    }
};