Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1025351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:54:54+00:00 2026-05-16T11:54:54+00:00

Here is the compilable code and the problem is still there #include <iostream> #include

  • 0

Here is the compilable code and the problem is still there

#include <iostream>   
#include <string>    

template<typename A,typename B,typename C>   
class Mesh{   
    public:   
    Mesh(){}   
    Mesh(std::string file){   
    A foo;   
    std::cout <<  file << endl;   
    }   
};    

template<typename A, typename B,typename C>    
class Eq{    
public:    
  Mesh<A,B,C>* pmesh;      
  Eq() {}
  Eq(Mesh<A,B,C> *pmesh_){
      pmesh = pmesh_;
  }
};

template<typename A, typename B,typename C>
class Pipe{
public:
  Mesh<A, B,C> mesh;
  Eq<A, B, C> eq1;

  Pipe(){}
  Pipe(std::string file){
      mesh = Mesh<A, B, C>(file);
      eq1 = Eq<A,B,C>(&mesh);
      std::cout << "P:"<<&mesh << " ";
  }
};

template<typename A,typename B, typename C>
class Simulator {
public:
    Pipe<A,B,C> pipe;

    Simulator(){}
    Simulator(std::string file){
        pipe = Pipe<A,B,C>(file);
        std::cout << "S:"<<&(pipe.mesh)<<" ";
    }
};

using namespace std;
int main() {
    typedef double A;
    typedef double B;
    typedef int  C;

    Simulator< A, B, C> simu("mesh");
}

The Outlet of the program is

mesh
P:0018FE44 S:0018FE3C

I think the problem is in the declaration of pipe, if I define sinulator as

template<typename A,typename B, typename C>    
class Simulator {   
public:
    Pipe<A,B,C>*  ppipe;

Simulator(){}
    Simulator(std::string file){
        ppipe = new Pipe<A,B,C>(file);
        std::cout << "S:"<<&(ppipe->mesh)<<" ";
    }
};

the output is

mesh
P:00308F08 S:00308F08

any idea, why the first code is wrong ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:54:55+00:00Added an answer on May 16, 2026 at 11:54 am

    The problem is with your copy in the Simulator constructor. This code should make things a bit more obvious.

    This has nothing to do with templates.

    #include <iostream>
    
    struct Mesh{ Mesh() { std::cout << "M:" << this << " ";} };
    
    struct Pipe{
      Mesh mesh;
    
      Pipe() { std::cout << "PX:" << &mesh << " "; }
      Pipe(int file){
          std::cout << "P1:"<<&mesh << " ";
          mesh = Mesh();
          std::cout << "P2:"<<&mesh << " ";
      }
    };
    
    struct Simulator {
        Pipe pipe;
    
        Simulator(){
            std::cout << "S1:"<<&(pipe.mesh)<<" ";
            pipe = Pipe(2);
            std::cout << "S2:"<<&(pipe.mesh)<<" ";
        }
    };
    
    int main() {
        Simulator simu;
    }
    

    output:

    :!./temp2
    M:0x7fffffffea97 PX:0x7fffffffea97   <-- the setup of Simulator, before the constructor
    S1:0x7fffffffea97
        M:0x7fffffffea96  <-- setup of the Pipe object, before the Pipe constructor
        P1:0x7fffffffea96 M:0x7fffffffea95 P2:0x7fffffffea96
    S2:0x7fffffffea97
    

    When you call the Simulator constructor, it first creates the object. Since one of the members is “Pipe pipe”, that object is created at memory location ea97. Then we explicitly call the Pipe(std::string) constructor to create another object.

    In the Pipe constructor, the same thing is happening. We already have a Mesh object at ea96, but we create another one (at ea95) and copy it into that location with the built-in copy function.

    Then we’re back to the Simulator constructor, where the newly created Pipe object is copied to the location of SimulatorObject.pipe (ea97).

    EDIT: removing a few more pieces that don’t matter… so you can see clearly when each constructor is being called.

    The syntax you’re looking for is probably

    struct Simulator {
        Pipe pipe;
    
        Simulator(): pipe(2){
            std::cout << "S1:"<<&(pipe.mesh)<<" ";
        }
    };
    

    This will initialize the Pipe object inside Simulator “in-place”. Note that this isn’t a really big deal. Your objects will copy when they’re supposed to.

    If you have an object that can’t be copied (it manages a database connection or something), you can forbid that by creating a copy constructor and an operator= overload that are both “private:”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am clueless here... 1: private static class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> { 2: String
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Instead of trying to trying to put my problem into words, here's some code
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Bumped into another templates problem: The problem: I want to partially specialize a container-class
I have some generics in the following code: public <T extends Comparable<T>> int insertionSort(T[]
I wrote code when one class has only constant access to its content, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.