2

Simple C++ Simulation of Java String Literal Pool

Hi,

I can't make a call from a private static variable in my MyString class. Any idea?

static void displayPool() {
    MyString::table->displayAllStrings();
}
StringTable* (MyString::table) = new StringTable();

both of those are declared in MyString class. table is a private variable.

Thanks.

EDIT: headerfile

#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
#define POOLSIZE 100

class StringTable {
 public:
    StringTable();
    int addString(const char *str);
    char* getString(int i);
    void deleteString(int i);
    void displayAllStrings();
    void addCount(int);
    void minusCount(int);
 private:
    char** array; //da pool
    int* count;
    int size;
    int numStrings;
};

class MyString {
 public:
   MyString(const char*);
   MyString(const MyString&);
   ~MyString();
   static void displayPool();
   MyString& operator=(const MyString &);
   char* intern() const;
 private:
   int length;
   int index;
   static StringTable* table;
   friend MyString operator+(const MyString& lhs, const MyString& rhs);
   friend ostream& operator<<(ostream & os, const MyString & str);
 }; 

#endif
4
  • what is the error from the compiler? Commented Oct 4, 2012 at 23:30
  • it just says error within this context. But when I tried MyString::table it says MyString::table is private Commented Oct 4, 2012 at 23:33
  • 1
    Paste the actual error message into your question. Just because the error is more-or-less meaningless to you (otherwise you would know what the problem is ;-]) doesn't mean it is to everyone else. Commented Oct 4, 2012 at 23:37
  • Show us the relevant class declaration code. Commented Oct 4, 2012 at 23:37

4 Answers 4

6
static void displayPool() {
    MyString::table->displayAllStrings();
}

This is not doing what you think it is doing. It is defining the free function displayPool. All that the keyword static does is to keep the function local to the source file in which the function is defined. What you want is to define the static member function MyString::displayPool():

void MyString::displayPool() {
    table->displayAllStrings();
}

The MyString:: before displayPool is essential. You do not want the static keyword here; adding that would be an error. Finally, note that there is no need for the MyString:: to qualify table. A static member function can see all of the static data members without a need for qualification. The only reason you would need to qualify table is if there was a global variable named table; then table would be ambiguous.

Sign up to request clarification or add additional context in comments.

Comments

1

What you want in this case is the following:

void MyString::displayPool() {
    MyString::table->displayAllStrings();
}

Comments

0

If you want a static variable inside your static function you should just do:

static void displayPool() {
    static StringTable* table = new StringTable();

    table->displayAllStrings();
}

However I have a feeling the problem might be asking you to create a static method for some class. You might want to re-read the problem.

2 Comments

We are trying to simulate Java string literal pool. The static variable was already declared. The purpose of this function is to display all the elements inside the table.
I think what you're asking then is to use a private static field from inside a static method. You need to make the method a static member of your class (and you will not want the static modifier on it which has a completely different meaning).
0

have you declared

StringTable* table;

in the class definition of MyString with the public access specifier?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.