Advertisement

01.13.2005 at 03:35PM PST, ID: 21273891
[x]
Attachment Details

How to port a Perl class containing a static hash of string => ref_to_array_of_strings pairs to C++?

Asked by craigmanley in Perl Programming Language

Tags: perl, class, hash, static

I'm new to C++, but basically what I want to do is create a C++ class that contains a private static const hash of string => array_ref_of_strings pairs and just dump it to stdout. Is not much to ask is it? I've create 2 examples of code below. This first one is the Perl example. The second one is my inexperienced idea of how it should be done in C++ which is probably full of inefficiencies and other weirdness. It works, but I'm not sure what I'm doing so please advise. I've added comments and questions to the C++ example. One thing I really want to know is how to declare a variable map (hash) variable as NULL and define it later (Test::_getFood()  implementation).

-Craig Manley.

============ This the example Perl script containing a Test class. ===============
#!/usr/bin/perl -w
use strict;

# main
my $t = new Test();
$t->Dump();
exit(0);


# Declare the Test class and it's methods.
package Test;

my $static_food;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  if (!defined($static_food)) {
    $static_food = {
                      'fruit'      => ['apples', 'bananas', 'oranges'],
                    'vegetables' => ['peas', 'carrots']
                   };
  }
  my $self = {'_food' => $static_food};
  bless($self,$class);
  return $self;
}

sub Dump {
  my $self = shift;
  my $food = $self->{'_food'};
  foreach my $key (keys %{$food}) {
    print "$key:\n";
    my $vals = $food->{$key};
    foreach my $val (@{$vals}) {
      print "\t" . $val . "\n";
    }
  }
}

1;



============ This how I ported it to C++. ===============
#include <map>
#include <vector>
#include <iostream>
using namespace std;

typedef map<string, vector<string> > MSVS;
typedef MSVS::iterator IMSVS;


//////// Interface ////////
class Test {
  public:
    // Contructor.
    Test();
    // Dumps contents.
    void dump();
  protected:
    // Returns a static 'food' map.
    MSVS _getFood();
};


//////// Implementation ////////

// Contructor.
Test::Test() {
}


// Dumps contents.
void Test::dump() {
  MSVS food = this->_getFood(); // Is this assignment efficient (i.e. just a pointer copy)?
  for (IMSVS iter = food.begin(); iter != food.end(); iter++) {
    cout << iter->first << ":\n";
    vector<string> values = iter->second; // Is this assignment efficient (i.e. just a pointer copy)?
    for (int i = 0; i < values.size(); i++) {
      cout << "\t" << values[i] << "\n";
    }
  }
}


// Returns a static 'food' map.
MSVS Test::_getFood() {
  // Why can't I do this?:
  // static MSVS food = NULL;
  // if (food == NULL) {
  //   food = new MSVS();
  //   ...
  // }
  //
  static bool initialized = false;
  static MSVS food;
  if (!initialized) {
    food["fruit"].push_back("apples");
    food["fruit"].push_back("bananas");
    food["fruit"].push_back("oranges");
    food["vegetables"].push_back("peas");
    food["vegetables"].push_back("carrots");
    initialized = true;
  }
  return food;
}



//////// Run it... ////////
int main() {
  Test* t = new Test();
  t->dump();
  return 0;
}


Start Free Trial
 
Loading Advertisement...
 
[+][-]01.13.2005 at 08:26PM PST, ID: 13041464

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 01:06AM PST, ID: 13042361

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 08:53AM PST, ID: 13045874

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Perl Programming Language
Tags: perl, class, hash, static
Sign Up Now!
Solution Provided By: bounsy
Participating Experts: 1
Solution Grade: B
 
 
[+][-]01.14.2005 at 11:18AM PST, ID: 13047662

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 12:25PM PST, ID: 13048303

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 02:25PM PST, ID: 13049658

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 02:40PM PST, ID: 13049800

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 02:58PM PST, ID: 13049940

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.14.2005 at 03:32PM PST, ID: 13050187

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32