ifstream from one class to another

Hello. Imagine a class named Poet, with a member FirstNAme, LastName and class named Verse as Poet's member. is it possible to construct Poet with ifstream, and after initializing FirstName and LastName then construct member Verse with the same ifstream?I mean, how can I pass ifstream correctly from one class to it's member class? Thanks.
My two pennyworth:

loremipsum.txt:

Lorem ipsum dolor sit amet, ei usu exerci vocibus invidunt, ea tantas civibus sed, ne vitae alterum probatus eam. Assum ornatus ius ea, autem commodo tamquam eam ex. Est habeo signiferumque an, ut his novum definitionem. Quod hendrerit omittantur ad duo, ei usu diceret delicata. Te cum populo scripta gubergren.

Duo vide consul et, ne est abhorreant moderatius. Option partiendo eos eu, per ea timeam mandamus mediocritatem. Nec at minim sonet consequuntur. Sea eu semper aliquid volumus, et solum copiosae pertinax eum. Possit dictas facilisis mei ex.

Mundi persius platonem no vim. Sea no aperiam omittantur. Ad vel sanctus utroque assentior, mei ei aeque suscipit patrioque. Alia mundi postulant nam eu, his at amet feugait hendrerit. Ex quis dicam aeterno sea, rebum recusabo et mel.

Ocurreret expetendis in vim, pro elitr ceteros at, iusto utamur cu qui. Pro at lorem commodo, cum summo melius nostrum ad, his fierent delectus et. Ne eum clita omnesque molestiae, illud etiam consetetur an usu. Illud gloriatur vix an, liber vitae vix id, vivendo phaedrum molestiae vis et. Nam at impedit accusamus, atomorum prodesset dissentias has ei, et purto noster eum.

Et quod dico erroribus nam, in eum possit fuisset. At ornatus principes est. Duo epicurei legendos ei. Ea vel oratio graece, mel te liber consul persecuti. Ad qui nobis tritani aliquando. Quas veritus dissentiet ei vis, probo intellegam duo an, sit id rebum tantas suavitate.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Hello. Imagine a class named Poet, with a member FirstNAme, LastName
// and class named Verse as Poet's member. is it possible to construct Poet
// with ifstream, and after initializing FirstName and LastName then
// construct member Verse with the same ifstream?I mean, how can I pass
// ifstream correctly from one class to it's member class? Thanks.
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>
#include <string>

struct Poet {
    std::shared_ptr<std::ifstream> in;
    struct Verse {
        std::shared_ptr<std::ifstream> in;
    };
    Verse verse;
};

void waitForEnter();

int main()
{
    Poet baudelaire;
    baudelaire.in = std::make_shared<std::ifstream>("loremipsum.txt");
    baudelaire.verse.in = baudelaire.in;
    for(std::string s; std::getline(*baudelaire.verse.in, s); /**/) {
        std::cout << s << '\n';
    }
    std::cout << '\n';
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

@Eniozat Thank you so much for answering my question! But I've already solved my problem, so will mark as solved. Thanks again :)
Registered users can post here. Sign in or register to post.